Input/Output to File Question

I am trying to make a playlist organizer as an exercise to learn more about i/o to files. I'm having lots of problems but the two I need help with is positioning where the program reads and writes from and strings with spaces.

I need the output to always start at the end of the file so it does not overwrite the past outputs to the file. I have looked at the reference here but I still don't know what to do. I then would have to reposition it at the begin to read from the file. Any help is greatly appreciated.

Another problem is that if someone inputs a string with more than one word it skips. Again, I'm clueless of what to do and any help would be gladly received.

Thanks in advance.
for your first question:
http://www.cplusplus.com/reference/iostream/fstream/open/
look at parameters specifically the app flag.

for your second:
http://www.cplusplus.com/reference/iostream/istringstream/
and a loop of some description.
I found a similar thread to the your first link but it didn't show the example which helped me solved it. Thanks :)

My actual problem with strings is not to do with i/o but simply writing to a string. When I write to a string with user input it skips it. Here's an example to explain what I mean:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>

int main()
{
    using namespace std;
    
    string fullName;
    
    cout << "What is your full name?\n> ";
    cin >> fullName;
    
    cout << fullName;
    
    cout << "Press ENTER to exit\n";
    cin.get();
    cin.get();
    return 0;
}


This doesn't work because everyone full name consists of at least two separate words. A song title can be a varying amount of word though so I need it to accommodate for that.
I used getline() which sort of worked.

1
2
3
4
5
6
cout << "Song Title:\t\t";                   
getline (cin,song);  //it prints out Artist before this line works           
cout << "Artist:\t\t";
getline (cin,artist); 
cout << "Album:\t\t";
getline (cin,album);


EDIT: When I put a breakpoint on the commented line above it skips it in the program.

EDIT: A further getline() got it to work although I have no idea why...
Last edited on
when you declare the string try using std::string song; ect. and remember to use #include <cstdlib> and #include <string> .
try cin.peek(), it returns 0 if there is nothing in the stream
1
2
3
4
5
6
7
8
9
10
11
12

std::stringstream stringer;
std::string string;

while(cin.peek())
{
   std::cin>>stringer;
   stringer<<' ';
}

string = stringer.str();
stringer.ignore(stringer.str().size());


I already got it to work but thanks for the replies.
Topic archived. No new replies allowed.