How to read a certain line of a txt file?

Let's say I have a text file that consists of 10 lines.
Line one reads "One", line two reads "Two" (without the quotation marks.) etc.

I know how to open the file in C++ and display the entire contents to the win32 console

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () 
{
  string line;
  ifstream myfile ("numbers.txt");
  if (myfile.is_open())
  {
    while (! myfile.eof() )
    {
      getline (myfile,line);
      cout << line << endl;
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 

  return 0;
}


but how do I use C++ to read, for example, only line 7?

Is it possible? Any examples you can link to/post that I can learn from?

Thanks.

If you don't know in advance how long the lines are, you'll just have to read all lines up to line 7 and discard these you don't need.
If all lines are the same length, you can seek to the position of the desired line directly.
Ok, interesting. Can you post a sample or link to one please?
(Or tell me the keyword to search for on Google?)

Thanks.
link to one

To one what?
There's nothing magic about it - just do what you're doing now, except that you keep track of the current line number.
To one what?
Went typing crazy, sorry ignore that.

I meant, is there a C++ "ignore" text-specific keyword I can Google and learn from?
1
2
3
    for (int lineno = 0; getline (myfile,line) && lineno < 7; lineno++)
      if (lineno == 6)
          cout << line << endl;
Topic archived. No new replies allowed.