Reading only part of the content in textfile

Hi all,

I have a text file containing these:

1
2
 csc : -212 : 21July2011
salary : 2000 : 28July2011 



I would like to only read the 2nd column of the content.
How do I do so?


Currently my code is:
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>


using namespace std;

int main()
{
    
          ifstream cashflow;
          cashflow.open ("cashflow.txt"); 
          string data;

          
          while (getline(cashflow, data))
          {
                cout << data << endl;
          }
          
          cashflow.close();
          
          system("pause");
          return 0;   
}



Any suggestion on how to read only the 2nd column instead of the whole line?
Thanks in advance :)
Last edited on
bump
I can read a specific column already.
But the output will always have a repeated values display out.

What I want to display is:
1
2
-212
2000



But the output is:
1
2
3
-212
2000
2000



Below is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 ifstream cashflow;
          cashflow.open ("cashflow.txt"); 
          string name, separator;
          int money;
          int total_incomes = 0;
          int total_expenses = 0;
          int netflow = 0;
          string columns[2];
          string enter;
       while(! cashflow.eof())  
       {  
          if(! cashflow.eof())
          {
             for(int x = 0; x < 2; x++)
             {   
             getline(cashflow, columns[x], ':'); 
             }   
          } 
           cout << columns[1] << endl; 
       } 



Please help!
I think you are just going to have to count the lines until you get to the one that you are interested in. You could avoid that if you made sure that your data file contained fixed length lines.
The data file is suppose to contain any amount of lines.
As long there is x amount of lines inside the data file, I will have to display all out.
Also:
1
2
3
4
5
6
7
8
9
10
11
       while(! cashflow.eof())  
       {  
          if(! cashflow.eof())
          {
             for(int x = 0; x < 2; x++)
             {   
             getline(cashflow, columns[x], ':'); 
             }   
          } 
           cout << columns[1] << endl; 
       } 

You should never loop using eof() as the condition of your while() clause. The reason being that EOF will not be flagged until after you try to read past the end of the file. This means that your getline() will fail (when it reaches the end of the file) but that you will still be using its values, that did not come from the file.
I have change it
1
2
3
4
5
6
7
8
9
10
11
while(cashflow.good())  
       {  
          if(! cashflow.eof())
          {
             for(int x = 0; x < 2; x++)
             {   
             getline(cashflow, columns[x], ':'); 
             }   
          } 
           cout << columns[1] << endl; 
       } 



But the same problem still exist.
The output display an additional last line.
You still have the same problem. When your getline(cashflow, columns[x], ':'); fails because it reached the end of the file you will still use its values even though they were never read. You need to check your stream *after* doing getline(), not before.
Thanks. My problem solved.

Topic archived. No new replies allowed.