0

I'm attempting to create a program that reads from a text file and carries out functions on values stored in different account numbers(represented by numbers 1-5) as well as using a character next to these account numbers to carry out certain functions(i.e. "1 D 200.00" signifies a 200 value addition to the value of account 1). The text file is formated in this manner

20000.00
10000.00
2500.00
67500.00
15000.00
2 W 200.00
4 D 50.00
1 B
2 U 
3 W 125.00

The first 5 values are scanned and are then used to signify the initial value of the accounts 1-5. Then each subsequent line read is meant to signify an action that is to be carried out on these now stored values. For example the line that reads "2 W 200.00" is to target account 2, use a withdraw function on this account, and use the float to modify the function( so 200.00 is subtracted from the value of account 2).

As a way to test that the values are being scanned and stored in the proper fashion, i'm first having the program run through a loop where it scans all of the values after the first 5 lines and print them. This is the code:

FILE*bankfile;
bankfile = fopen("bankfile.txt","r+");

for(i = 1; i < 15; i++)
    {
        if(i < 6)
        {
            fscanf(bankfile,"%f",&accValue);
        switch(i)
            {
                case 1:
                    acc1 = accValue;
                    break;
                case 2:
                    acc2 = accValue;
                    break;
                case 3:
                    acc3 = accValue;
                    break;
                case 4:
                    acc4 = accValue;
                    break;
                case 5:
                    acc5 = accValue;
                    break;
            }
        }
        else
        {
            fscanf(bankfile,"%d %c %f",&accNum,&accAction,&actValue);

            printf("%d %c %f\n",accNum,accAction,actValue);

        }



    }
    printf("Exited Loop");

When I run this code, the first two lines print as I would expect, but once the first line is reached that doesn't contain the float value that is being scanned, the program will go on to print the same value over and over until the loop is broken. This is what is printed:

2 W 200.00
4 D 50.00
1 B 2.00
1 B 2.00
1 B 2.00

etc until the program is finished.

Is there some way to get this program to recognize that a value for actValue does not exist for that line and the continue on to the next line?

Ezri
  • 7
  • 4
  • 3
    That's how scanf family functions work. They don't move the file pointer forward, when there's no match for the format specifier. Use `fgets()` and `sscanf()` instead. – P.P Mar 12 '15 at 06:58

1 Answers1

2

According to the documentation of fscanf() :

the function fscanf() returns the number of items of the argument list successfully filled. This count can match the expected number of items or be less (even zero) due to a matching failure, a reading error, or the reach of the end-of-file.

You can test the return value of fscanf() and exit the loop if it did not manage to read the expected number of items.

Input files may be faulty : testing this value allows you to print error messages if reading fails.

At lines of your file :

1 B
2 U

The program reads 1 B 2, then it tries to read U as an integer and it fails.

As newlines are a key part of your file format, follow the advice of @BlueMoon : use fgets() to read the entire line then parse the line using sscanf().

Do not use sscanf() many times on the same string or read Use sscanf to read multiple times string

You can also test the return value of sscanf() to check that the line was successfully read.

Community
  • 1
  • 1
francis
  • 9,525
  • 2
  • 25
  • 41