eveo
eveo

Reputation: 2833

Making array from file then printing said array

I scan from a text document the following values into various arrays:

0001:Satriani:Joe:6:38.0
0002:Vai:Steve:1:44.5
0003:Morse:Steve:10:50.0
0004:Van Halen:Eddie:3:25.75
0005:Petrucci:John:8:42.25
0006:Beck:Jeff:3:62.0

I do this with the following while loop:

while (fscanf(employeesTXT, "%d:%[^:]:%[^:]:%d:%lf\n", &empID[i], lastName[i], firstName[i], &payGroup[i], &hoursWorked[i]) != EOF)
{
    i++;
}  

This runs without apparent error. The problem occurs when I try to display that data again. I attempted it with this for loop:

for(i = 0; i < 16; i++)
{
    printf("%04d - %s - %s - %d - %.2lf\n", empID[i], lastName[i], firstName[i], payGroup[i], hoursWorked[i]);
}     

However, instead of the expected result, I get the following:

4587591 -  -  - 0 - 0.00
0006 -  -  - 0 - 0.00
0052 -  -  - 0 - 0.00
0052 -  -  - 0 - 0.00
0052 -  -  - 0 - 0.00
0320 -  -  - 0 - 0.00
0320 -  -  - 0 - 0.00
0005 -  -  - 0 - 0.00
0004 -  -  - 0 - 0.00
0003 -  -  - 0 - 0.00
0001 - Satriani - Joe - 6 - 38.00
0002 - Vai - Steve - 1 - 44.50
0003 - Morse - Steve - 10 - 50.00
0004 - Van Halen - Eddie - 3 - 25.75
0005 - Petrucci - John - 8 - 42.25
0006 - Beck - Jeff - 3 - 62.00  

What's causing all of the junk at the beginning, and is there anything I can do to remove it?

Upvotes: 1

Views: 65

Answers (1)

Karl Bielefeldt
Karl Bielefeldt

Reputation: 49018

Most likely you never initialized i to zero before doing the fscanf loop.

Upvotes: 4

Related Questions