1

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?

1
  • mark Q as solved if it's solved. Commented Apr 13, 2012 at 23:01

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

1 Comment

Haha I'm such an idiot. Thanks. I can't believe it's that simple sometimes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.