-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.

Young KIm
  • 15
  • 3
  • 1
    Note that [`while (!feof(file))` is always wrong!](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) – Jonathan Leffler Aug 12 '18 at 05:30
  • Actually, no, we can't see that the scanning part is not working. What did you give as input — what was in the file — how do you know — how are we supposed to know? How do you know it isn't working? Why aren't you testing the result from `fscanf_s()`? – Jonathan Leffler Aug 12 '18 at 05:34
  • I used the file i created in the first part as my input file. I tested fscanf_s() without the feof loop. Same problem happens. – Young KIm Aug 12 '18 at 05:37
  • Please add the extra information to the question — where you can format it cleanly. Comments have very limited formatting. – Jonathan Leffler Aug 12 '18 at 05:37
  • What is `size`? – qrdl Aug 12 '18 at 05:47
  • What is the value of `size` used to declare your array: `struct Employee guys[size];`? The chances are you are writing out of bounds of the array, but it's hard to know since the code isn't an MCVE ([MCVE]). It also is hard to compile _as is_ except on a Windows machine. The `_s` 'safe' functions are not generally available except on Windows. You have remembered to provide the length parameter for the string input; that's good. But you aren't checking that `i` is less than `size`, for example. – Jonathan Leffler Aug 12 '18 at 05:48
  • 1
    `scanf` does return a value - perhaps read the manual page. – Ed Heal Aug 12 '18 at 05:49
  • sorry guys i forgot to put the very beginning of the code. I set size to be 10 – Young KIm Aug 12 '18 at 05:53
  • What other stuff you missed out when doing the copy and paste? – Ed Heal Aug 12 '18 at 06:03
  • hopefully nothing haha – Young KIm Aug 12 '18 at 06:09
  • Start by checking return value of scanf. When you have code which does not work as expected, that's always step 1: verify that you check return codes of sll library functions for errors (plain printf is maybe the only valid exception to this rule....). – hyde Aug 12 '18 at 09:46

1 Answers1

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++;
}
kiran Biradar
  • 12,700
  • 3
  • 19
  • 44