How to read data from a file line by line in C++

I have an external file with one column of data. If I have a counter value let say counter =1, and counter++ and so on. How I can write such a c++ code that if the value of counter and value from the external file are same then generate an action let say cout both values i.e. value of counter and value from external file.

for more information, here is an example:

data in file(in one column): 2 6 8 9 10...
value of counter : 1 2 3 4 5 6 7 8 9...
then cout values only if value of counter and value from the file is same.

I hope I explained well. Please suggest how I can write c++ code with an example. Thank you very much. :)

here is my code so far, but it does not seem to work;

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
27
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
const int SIZE = 10; //Size declaration of array
int hours[SIZE]; //Array declaration
int count; //Loop counter variable
int i;
ifstream inputFile; //Input file stream object

inputFile.open("adil"); //Opening the file
if(!inputFile) cout << "there's something wrong with the file!\n"; //Throw exception

//Read numbers from file into the array
for(count = 0; count < SIZE && inputFile >> hours[count]; count++);

//Close the file
inputFile.close();

for(i=0; i< SIZE; i++){
if (i==hours[i])
{
    cout<<hours[i]<<endl;
}
}
}
Firstly, you need to read from a file and stop reading once the file hits the end.
use while loop to do so. Hint:
 
while ( !inputFile.eof() )


Then read the file line by line and store the line in a temp variable. Then insert if condition to compare between the counter and temp variable.
pseudo code:
1
2
3
4
5
6
7
8
9
10
open file 
while ( read until hits the end of file )
{
      read the file line by line and store it in a temporary variable
      if ( the counter equals the temporary variable )
      {
          Do some action;
      } 
      increment the counter;
}


However, you need to clarify what you want to compare? each value of the counter with the entire values of the file OR value by value?
the example you gave
data in file(in one column): 2 6 8 9 10...
value of counter : 1 2 3 4 5 6 7 8 9...


value of counter       value of the file
1                         2
2                         6
3                         8
4                         9
6                         10


As you see there is no match unless you want to compare each value of the counter with the entire data of the file.
Last edited on
Every time I see "while (!eof)" I die a little inside...

The correct code is:

1
2
3
4
5
6
ifstream inputfile( ... );
string s;
while (getline( inputfile, s ))
  {
  // do something with s
  }


However, that said, since your file is a single column of numbers, you don't actually have to read by lines, you can read the numbers directly:

1
2
3
4
5
6
7
8
9
10
ifstream inputfile( ... );
counter = 1;
int n;
while (inputfile >> n)
  {
  if (counter == n)
    {
    // do something
    }
  }

Hope this helps.
Yeah, I guess the while (!eof) thing is a hangover from C, where files were read 1 char at a time, and there was an ungetc function as well.

K&R had examples like that early in their book, but later showed how to implement getline.
while(!feof(file)) is an error in C just the same way.
I didn't explain myself very well, to be clearer, I should have said that K&R had this :

while ((c = getchar()) != EOF)

Thanks a lot for quick replies. I am sorry I didn't explained counter well. To explain in simple words, My counter should run from 1 to 20. where as data in the external file is although in ascending form but not in any order. for example;
Counter values should be= 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20

and Data in external file is like this (less values than 20);
2
4
6
7
8
10
13
14
15

I want to execute an action when my counter value reaches equal to any integer from external file and a different action if it does not. Here is my so far code, but it is not running as expected. I am sorry for any stupid mistakes, I am new to programming. Cheers :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
#include<fstream>
using namespace std;

int main()
{
       ifstream iFile("adil");	// input.txt has integers, one per line
    while (!iFile.eof())
    {
     int x=0;
     iFile >> x;
     counter =0;
    if (counter == x){
        cout<<"value of counter is "<< counter<< " and value of array data is "<<x<<endl;
        }
        else{
        cout<<"counter does not match with data "<< counter<< " , " << x <<endl;
    }
        counter ++;
    }
    iFile.close();
return 0;
}
I will repeat my question again. you need to clarify what you want to compare? each value of the counter with the entire values of the file OR value by value?
See your last example,

value of counter       value of the file
1                         2
2                         4
3                         6
4                         8
6                         10
7                         11
8                         12
9                         13
10                         14
11                         15


There is no match whatsoever.
Also, the counter = 0 should be out of the while Loop. Every iteration you reset the counter to be zero.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
while (!iFile.eof())
    {
     int x=0;
     iFile >> x;
     counter =0;  // <---------- put it before starting the while loop
    if (counter == x)
    {
        cout<<"value of counter is "<< counter<< " and value of array data is "<<x<<endl;
     }
        else
       {
        cout<<"counter does not match with data "<< counter<< " , " << x <<endl;
        }
        counter ++;
    }
Last edited on
@Duoas,
Every time I see "while (!eof)" I die a little inside...


Would you please explain why this should be avoided?
@CroCo Thanks for reply. ya i want to compare value of counter with the entire values of file. If true then make an action and if false then make another action. Sorry once again for ambiguity.

counter     file      action I or II
1              1           action I
2              2           action I
3              3           action I
4                           action II
5                           action II 
6              6           action I
7              7           action I
8                           action II
9                           action II          
10                         action II
11                         action II
12            12         action I 


^_^ cheers!
Last edited on
Ok then, you need two while loops.
Pseudo Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
set the counter;
while ( counter <= the desired value )
{
     while ( read data from a file until hits the end of the file and store them in Temp )
     {
         if ( counter == Temp )
         {
            Do action;
         }
         else
         {
            Do another action;
         }
     }
     ++counter;
}
@cire ,
Thanks
Topic archived. No new replies allowed.