Read a specific line in ifstream.

I've been looking around and I keep running into the getline() command. I basically have a txt file with several lines. I want to be able to tell it a line number, such as 5 and have it go to line 5 and save the entire line to a string for example that it can display.How can i do this? Thanks for help.
Perhaps you could try the ignore function? Use '\n' as the delimiter and ignore 4 times. then use getline to read the 5th line.

Invoke this on your file stream object 4 times. This should advance the stream position to the 5th line. Then use getline to read the 5th line. I think that might work but I have not tried it.
ignore(std::numeric_limits<std::streamsize>::max(), '\n');
i do not think that there is such a function that does exactly what you want. If my idea works you could write your own wrapper function such that it takes a line number advances the stream to that line, reads it, and returns a string. One way or another you have to advance the file stream position on your own in order to get to the line that you want to read.
Topic archived. No new replies allowed.