I am trying to get a specific line in a text file which only has one word per line and copy it to a string. Can anyone give a simple example?
thanks
Printable View
I am trying to get a specific line in a text file which only has one word per line and copy it to a string. Can anyone give a simple example?
thanks
You cannot "get" it directly from a file.
You should read the whole file line-by-line and only save the lines you do need.
thanks I figured that one out, how about writing a word to the line you want.
Is it possible to post an example please?
thanks
The same way: read the file, change the line you want to, then write in to the file back (overwriting it)
This is what I used to read a specific line. :
I need to overwrite that line now, can you post some hints pls.Code:ifstream f("input.txt");
string s;
getline(f, s);
getline(f, s);
getline(f, s); //here is 3rd line
f.close();
cout << s << endl;
thanks
You cannot overwrite "one line only". You have to overwrite the whole tail of the file beginning with this line.
Where can I find more examples using the fstream and familiarize myself with it?
thanks
1. MSDN
2. Google
3. CG Forums.
Just search for the appropriate examples/samples...