how do read the 1st line in text file?

Hi,

Can someone tell me how do we read the first line in a text file?

As far as I know,the code below will read the number of line that are present in the text file. But how do I alter this code so that I can only read the first line in the text file.


while (is.good())
{
c=is.get();
if(c=='\n')
num++;
}
is.close();
}

Thanks in advance.
1
2
string line;
getline(stream,line);


See: http://www.cplusplus.com/reference/string/getline/
I assume line is the first line?
Yes, if you're at the beginning of the file when calling getline.
Ok.. But do I have to include the code that I gave it just now?
And what does 'stream' mean?
No. You should strive to understand the pieces of code you copy and paste from God-knows-where.
The C++ reference on this site (or rather, Google) will help you with that. As will a good book.
Right..... Then can you suggest me which book should I look into? I'm interested in buying one.

I understand how does the above code work. I am just clueless on where should I put the getline thing as I am really new in this C++. And I also don't really understand what does getline do.
I suggest the C++ Primer:
http://www.amazon.com/Primer-4th-Stanley-B-Lippman/dp/0201721481

What getline does is explained in the reference. The abridged version that it reads a line from the stream and stores it in the passed string.
Topic archived. No new replies allowed.