Home > Enterprise >  Why my fscanf does not read the first string but reads the last line into the first string instead?
Why my fscanf does not read the first string but reads the last line into the first string instead?

Time:12-01

I'm a beginner at files including code so please someone help me. This is what my code looks like.

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define first_ten_hours 7.99;
#define after_ten_hours 1.99;

double charges_all(int hour);
double charges_avg(int money, int hour_use);
double round_money(double sum);

int main()
{
    FILE *f_read, *f_write;

    f_read = fopen("usage.txt", "r");
    f_write = fopen("charges.txt", "w");

    if(f_read == NULL)
    {
        printf("Error to open the file.\n");
        return 1;
    }

    char date[2][10];
    char studenid[10][100];
    double using_hour[10];
    int current_line = 1;

    for(int i = 0; i < 11; i  )
    {
        if(current_line == 1)
        {
            fscanf(f_read, "%s %s", date[0], date[1]);
            current_line  ;
        }
        else
        {
            fscanf(f_read, "%s %lf", studenid[i], &using_hour[i]);
            current_line  ;
        }
    }

    current_line = 1;

    for(int i = 0; i < 11; i  )
    {
        if(current_line == 1)
        {
            printf("%s %s\n", date[0], date[1]);
            current_line  ;
        }
        else
        {
            printf("%s %lf\n", studenid[i], using_hour[i]);
            current_line  ;
        }
    }

    fclose(f_read);
    fclose(f_write);

    return 1;   
}

After the current_line = 1;, I print to check and see the problem.

This is my text file.

12 2022
18010 4.5
92052 3.2
01051 7.4
11052 6.3
13052 5.5
01081 2.2
65052 1.3
94052 2.8
32052 3.7
41051 4.9

and this is the output from the printf loop:

41051 2022
18010 4.500000
92052 3.200000
01051 7.400000
11052 6.300000
13052 5.500000
01081 2.200000
65052 1.300000
94052 2.800000
32052 3.700000
41051 4.900000

But what I want is for the first string at the first line to be 12.

CodePudding user response:

Because of the way you have written your for loop(s), you are accessing elements of the studenid and using_hour arrays that are out-of-bounds on the last iteration of those loops (i.e. when i is 10 – an element at [10] is past the end of an array declared with 10 elements). Such "bad access" causes undefined behaviour, which may (as it appears in your case) include overwriting the value you had previously read into the date array elements.

A "quick fix" is to change the [i] indexes (all of them) to [i - 1]. However, a better way would be to put the read of the first line outside the loops and then run those loops for the next 10 lines:

int main()
{
    FILE* f_read, * f_write;

    f_read = fopen("usage.txt", "r");
    f_write = fopen("charges.txt", "w");

    if (f_read == NULL)
    {
        printf("Error to open the file.\n");
        return 1;
    }

    char date[2][10];
    char studenid[10][100];
    double using_hour[10];

    fscanf(f_read, "%9s %9s", date[0], date[1]); // Read first line differently
    for (int i = 0; i < 10; i  ) // Run the loop ONLY 10 times ...
    {
        fscanf(f_read, "           
  • Related