Archived

This topic is now archived and is closed to further replies.

getline() help please

This topic is 5026 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey Im writing a program to use getline() in a text file but so far all I can put into a string is the very first line of the file....what if i wanted the second or third? or even the entire file? here is a simple prog to demo what i mean:
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void main() {

string s1;
ifstream ifs("C:/Program Files/Blah.txt");
getline(ifs, s1);
cout << s1;
}
any help? thanks! [edited by - RedRabbit on April 13, 2004 8:36:00 PM]

Share this post


Link to post
Share on other sites
All you have to do is call getline again to get the next line.

tj963

Share this post


Link to post
Share on other sites
To get the second line, call getline twice, discarding the first return value. To get the third line, call getline three times, discarding the first two return values. To get the entire file, call getline in a loop, storing the returned line of text into a buffer or queue or list or vector; or read the entire file buffer at once.

You could always seekg within the fstream, if you know how far you want to advance...

Share this post


Link to post
Share on other sites