STL jump to line

R

raven.mp4

Hi. Is there any function like getline which loads specified line from
a text file? Something like getline(myfile, line, 10); where 10 means
10th line in the file.
 
V

Victor Bazarov

Hi. Is there any function like getline which loads specified line from
a text file? Something like getline(myfile, line, 10); where 10 means
10th line in the file.

No. It's too easy to write using 'getline' and 'ignore' to bother
putting it in the library, methinks.

V
 
V

Victor Bazarov

So how to code it?

Something like

std::istream& skip_lines(std::istream& is, std::size_t n)
{
while (is && n--)
is.ignore(
std::numeric_limits<std::istream::streamsize>::max(),
'\n');
return is;
}

...
std::ifstream myfile; // and open it
if (skip_lines(myfile, 10))
getline(myfile, line);

V
 
J

Jerry Coffin

Hi. Is there any function like getline which loads specified line from
a text file? Something like getline(myfile, line, 10); where 10 means
10th line in the file.

No -- to do something like that, you'd need an index of the text file
telling where each line begins. You can do that, but it's not built in.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,290
Messages
2,571,453
Members
48,129
Latest member
DianneCarn

Latest Threads

Top