How to access a specific line in a file

How do I read only a specific line in a txt file and save the number in the next line in a variable?
I'm not sure I quite understand your question. What have you tried already? What is the real problem or error you are getting? As far as reading a specific line, take a look at this, it is taken from the Input/Output with files tutorial hosted on this site:

1
2
3
4
5
6
7
8
  string line;
  ifstream myfile ("example.txt");
  if (myfile.is_open())
  {
    while ( getline (myfile,line) )
      cout << line << '\n';
    myfile.close();
  }


Notice it gets the file line by line via getline(myfile, line).
With this information you could simply setup a for loop to loop to the line you need.
The user of the program gives you certain informations about a project and the program saves them in a txt file like this:

*** Internal code: **
1
Project Title:
nju
Institution:
kmik
Required Funding:
13950
Grade:
B
Given funds:
10462.5

If you add another project, the information is saved in the same file. What I want to do is retrive the internal code of each project and save it in a variable, and return the title of the projects.
Last edited on
The above code should assist you then, what all have you tried. I cannot just provide the code for you. I need to know that you have actually tried this.
Topic archived. No new replies allowed.