Scan value from a text file into structure

-1

I am having a problem with the fscanf_s function. I made a code which it asks an user if they have a file for data input, and if they do it reads the data and get the values into the structure. Well, it doesn't work. I tried to find an error by myself, but I failed, so I'm here looking for some help.

The first part where the user enters input and the programs creates a text file based on the input works, but as you can see the scanning part is not working. Any help will be great.

  #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define size 10
char *filename = "file.txt";
    char savefile[20];

    struct Employee
    {
        char name[20];
        float rate;
        float hours;

    }employee; 


    int main(void)
    {

        FILE *file;
        FILE *output;
        FILE *input;
        int count = 0;
        int yn;
        int x = 0; int q = 0;
        int o = 0; int k = 0;
        int y = 0; int i = 0;
        struct Employee guys[size];



        printf("Do you have a file? 1. Yes 2. No\n");
        scanf_s("%d", &yn);
        if (yn == 2)
        {   

            errno_t err = fopen_s(&file, filename, "w");

            for (q = 0; q < size; q++) 
            {
                puts("\ntype name: (type -1 to quit) \n");
                scanf_s("%s", &guys[q].name, 20);
                if (strcmp(guys[q].name, "-1") == 0) { break; }

                puts("\ntype hourly rate: (type -1 to quit)\n");
                scanf_s("%f", &guys[q].rate);
                if (guys[q].rate == -1) { break; }

                puts("\ntype hours worked: (type -1 to quit)\n");
                scanf_s("%f", &guys[q].hours);
                if (guys[q].hours == -1) { break; }
                count++;
            }

            for (int q = 0; q < count; q++)
            {
                fprintf(file, "%s %f %f\n",  guys[q].name,guys[q].rate, guys[q].hours);
            }
            fclose(file);

        }

        if (yn == 1)
        {
            errno_t err = fopen_s(&input, filename, "r");

            if (err != 0)
            {
                printf("Unable to open up %s", filename);
                return;
            }

            while (!feof(input))
            {
                fscanf_s(file, "%s" "%f" "%f", &guys[i].name,20,&guys[i].rate, &guys[i].hours);
                i++;
            }
            fclose(input);
        }

I used the file i created in the first part as the input for the second part. I also tested fscanf_s() without feof loop. Same problem still happens. the error keep popping says Exception thrown at 0x77506165 (ntdll.dll) in program05.exe: 0xC0000005: Access violation writing location 0xCCCCCCF0.

c
asked on Stack Overflow Aug 12, 2018 by Young KIm • edited Aug 12, 2018 by Young KIm

1 Answer

0

There are two problems in your read code which leads to crash.

  1. You are passing closed file pointer to fscanf_s function.

    fclose(file);

  2. You are passing invalid address 20 to fscanf_s function.

       fscanf_s(file, "%s" "%f" "%f", &guys[i].name,20,&guys[i].rate, &guys[i].hours);
    

    should be changed to

     fscanf_s(input, "%s" "%f" "%f", &guys[i].name,&guys[i].rate, &guys[i].hours);
    

I would like to suggest you to not use

while (!feof(input))

Instead go with the below approach.

while (fscanf_s(input, "%s %f %f", &guys[i].name,&guys[i].rate, &guys[i].hours) == 3)
{
    i++;
}
answered on Stack Overflow Aug 12, 2018 by kiran Biradar • edited Aug 12, 2018 by kiran Biradar

User contributions licensed under CC BY-SA 3.0