|
-
November 3rd, 2011, 02:22 PM
#1
Get specific line from text file
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
-
November 3rd, 2011, 02:49 PM
#2
Re: Get specific line from text file
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.
Victor Nijegorodov
-
November 3rd, 2011, 03:27 PM
#3
Re: Get specific line from text file
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
-
November 3rd, 2011, 03:44 PM
#4
Re: Get specific line from text file
The same way: read the file, change the line you want to, then write in to the file back (overwriting it)
Victor Nijegorodov
-
November 4th, 2011, 04:08 AM
#5
Re: Get specific line from text file
This is what I used to read a specific line. :
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;
I need to overwrite that line now, can you post some hints pls.
thanks
-
November 4th, 2011, 04:26 AM
#6
Re: Get specific line from text file
You cannot overwrite "one line only". You have to overwrite the whole tail of the file beginning with this line.
Victor Nijegorodov
-
November 4th, 2011, 04:43 AM
#7
Re: Get specific line from text file
Where can I find more examples using the fstream and familiarize myself with it?
thanks
-
November 4th, 2011, 04:57 AM
#8
Re: Get specific line from text file
1. MSDN
2. Google
3. CG Forums.
Just search for the appropriate examples/samples...
Victor Nijegorodov
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|