Reading from a txt file

hey people can someone please tell me how to omit reading the headings in a txt file?

Last edited on
a) Use seekg() if you know position where you should start rading from
b) Use getline() to some temporary variable as many times, as there header lines
imagine this is the txt file

phase 1
name age salary
kk 12 1200
ll 13 1300

phase 2
name age salary
kk 12 1500
ll 13 1900

in this how to avoid the headings and store the details in array?
Assuming that the name does not contain white spaces, something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <fstream>
#include <string>

int main()
{
     std::ifstream file( "myfile.txt" ) ;

     std::string throw_it_away ;
     // for each phase
     while( std::getline( file, throw_it_away )
             && std::getline( file, throw_it_away )  ) // throw away the first two lines
     {
         // read in name, age, salary in a loop
         std::string name ;
         int age ;
         double salary ;
         while( file >> name >> age >> salary )
         {
             // do whatever is needed with name, age and salary
         }

         // reset the failed state of the stream
         file.clear() ;
     }
}
Topic archived. No new replies allowed.