looping file read wont find eof

I'm trying to read in a large file containing voting data stored as candidate,precinct ex. B,4 and keep track of the votes in a 2d array, but for whatever reason the variables I'm reading in stay "locked" to the 1st line and it wont find the eof. I think it just keeps reading the 1st line over and over. no idea why.


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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const int CANDIDATES = 4;
const int PRECINCTS = 5;

// reads in the file containing votes and populates a 2-D array with the data
void voteRead(int votes[PRECINCTS][CANDIDATES])
{           
    char cand;
    int prec, y, x, i = 0;
    
    // opens vote log file
    ifstream inFile;    
    inFile.open("votingdata.txt");
    
    // checks to see if the file opened correctly    
    if (!inFile)
       cout << "Error opening file\n";
    else
    {
        // reads in all the element data
        while(!inFile.eof())
        {
           cout << "loop " << i << endl;

           inFile >> skipws >> cand;
           inFile >> skipws >> prec;

           switch(cand)
           {
              case 'A':
                    y = 0;
                    break;
              case 'B':
                    y = 1;
                    break;
              case 'C':
                    y = 2;
                    break;
              case 'D':
                    y = 3;
                    break;
           }
           cout << cand << endl;            
           
           x = (prec - 1);
           
           cout << x  << " " << y << endl;
           
           votes[x][y]++;    
      
          i++;

        }        
        // closes the file
        inFile.close();    
    

    // check to see whats in the arrays
    for (int i = 0; i<CANDIDATES; i++)
    {
        for (int j = 0; j < PRECINCTS; j++)
        {
            cout << votes[j][i] << "  ";
        }
        cout << endl;
    }   
    } 
};


this is a snipit of the votingdata.txt
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
28
29
30
31
32
33
34
35
36
37
38
39
40
A,4
B,1
B,4
C,3
A,4
A,2
C,4
C,2
B,2
B,5
C,2
D,5
B,2
C,1
B,2
C,4
B,4
D,4
D,2
D,4
B,5
D,5
D,3
A,2
B,4
B,1
D,2
D,2
B,4
A,2
D,4
A,2
C,2
C,1
B,5
A,1
A,3
D,1
D,4
C,3
Last edited on
Try reading this, It helped me a lot. http://augustcouncil.com/~tgibson/tutorial/iotips.html
Its not the same format as what you did, but it seems to work for me, and will probobly work for what you want to do also.
Last edited on
I tried setting it up like they did in the link, but I was still getting stuck in an infinite loop
When using the extraction operator (>>) input works like so:

if there is leading whitespace, advance to the next input in the buffer
if that input is not what we expect it to be return to the where we were before we started.
else extract the input.

So, when you read the last valid input from the file, if there is any whitespace before the eof indicator, then the extraction operator will never cause the stream to reach the indicator.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
		bool done = false ;
		// reads in all the element data
		while(!done)
		{
			cout << "loop " << i << endl;

			inFile >> skipws >> cand;
			inFile >> skipws >> prec;

			if ( !inFile )
			{
				done = true ;
				continue ;
			}

			switch(cand))


Checking for eof is rarely the right thing to do, anyway.
Your program enters an endless loop:

Given the input "A,4\nB,1...", the line inFile >> skipws >> cand; processes the 'A', leaving ",4\nB,1..." in the input
Then the line inFile >> skipws >> prec fails, because comma is not allowed in an integer. Once it failed, inFile.fail() is true and no further reading from inFile is allowed.

Your loop condition is while(!inFile.eof()), which is an error to start with, but in this particular case it is never fulfilled because the input stream never reaches the end of file.

Replace

1
2
3
4
5
6
        while(!inFile.eof())
        { 
           cout << "loop " << i << endl;

           inFile >> skipws >> cand;
           inFile >> skipws >> prec;

with

1
2
3
4
        char comma;
        while(inFile >> cand >> comma >> prec && comma == ',')
        { 
           cout << "loop " << i << '\n';


There are other issues with style, etc, but this is enough to get sane results, given a zero-initialized array.
Last edited on
thank you Cubbi that fixed it.
Topic archived. No new replies allowed.