0

I have a structure like this

struct employeeRec{
        char employeeCode[20];
        char employeename[50];
        int salaryLevel;
        float salaryRate;
};

and in one of my conditions to input data on a structure, I did this. This includes writing it on a file.

        printf("Enter the Employee Code:\n");
        scanf("%s",emp.employeeCode);
        fflush(stdin);
        printf("Enter the Employee Name\n");
        scanf ("%[^\n]%*c", emp.employeename);
        fflush(stdin);
        printf("Enter the Salary Level:\n");
        scanf("%d", &emp.salaryLevel);
        printf("Enter the Salary Rate:\n");
        scanf("%f", &emp.salaryRate);
        ...
        fwrite(&emp,sizeof(emp),1,fp);

But when I try to read the file

    printf("Employee Name: %s\n",emp.employeeCode);
    printf("Employee Code: %s\n",emp.employeename);
    printf("Salary Level: %d\n",emp.salaryLevel);
    printf("Salary Rate: $ %f/day\n", emp.salaryRate);

Employee Name, Employee Code displays. But in my Salary rate, it only displays $0.0000000

I tried putting a printf right after the scanf during the inputting of salary rate and correct value appears. But it is during the reading of the file that the value becomes $0.0000000

Niic
  • 175
  • 1
  • 1
  • 11
  • Check the return values of `scanf`. It returns the number of items successfully read, which should be 1 in your examples. – Ian Abbott Jun 09 '21 at 13:30
  • 2
    What are you trying to achieve by doing `fflush(stdin)`? – babon Jun 09 '21 at 13:39
  • @babon: The first `fflush(stdin)` on WIndows removes the newline left behind by the prior `scanf()`. It would be better to put a space in the format string before the scan set — `" %49[^\n]%*c"` (where the 49 prevents buffer overflows). The second is pretty close to pointless even on Windows. See [Using `fflush(stdin)`](https://stackoverflow.com/q/2979209/15168) for more information on `fflush(stdin)`. – Jonathan Leffler Jun 09 '21 at 13:46
  • Why aren't you initializing the salary level? – Jonathan Leffler Jun 09 '21 at 13:47
  • @IanAbbott I tried putting printf("%f", &emp.salaryRate); after scanf("%f", &emp.salaryRate); and Yes, it does display the value I just inputted. The problem is, after reading the file, emp.salaryRate is 0 value – Niic Jun 09 '21 at 13:48
  • @JonathanLeffler forgive me, I wasn't able to include it in the inputting. Regardless whether it's included or not, employee code, employee name, salary level display the correct information except for salaryrate. – Niic Jun 09 '21 at 13:50
  • @JonathanLeffler that was true at one time, but [MS now says](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/fflush?view=msvc-160) *All streams opened in write mode and all streams opened in update mode where the last operation was a write are flushed. **The call has no effect on other streams.*** – Weather Vane Jun 09 '21 at 13:52
  • It would be simplest just to remove the member from the structure. It isn't likely to be a factor in your problem. What might be a factor is that you don't check that `scanf()` or `fwrite()` work. You don't show the `fread()` call (presumably) that reads the data; you don't show the checking of the `fread()` call. – Jonathan Leffler Jun 09 '21 at 13:52
  • You should show the code including the `printf` that shows the correct value and including the part that reads the data from the file. If possible show a hex dump of the file. Create a [mre]. If you think the problem is not caused by the input, then you could replace this with hard-coded values. – Bodo Jun 09 '21 at 13:53
  • @JonathanLeffler I just removed the member from the struct and I'll just store it in another variable instead. – Niic Jun 09 '21 at 14:03
  • 1
    Was the file associated with the `fwrite` call opened in binary mode (e.g. `"wb"`)? And was the file associated the the unshown `fread` call also opened in binary mode (e.g. `"rb"`)? It doesn't make a difference on Unix-like platforms, but it certainly does make a difference on Windows. – Ian Abbott Jun 09 '21 at 14:04
  • @IanAbbott Initially suspected this was the reason. I had both 'ab' AND 'rb'. Switched to 'a' and 'r' but still had the same problem. – Niic Jun 09 '21 at 14:05
  • When reading or writing binary data such as `float` values, use `"rb"` or `"ab"` or `"wb"` as the mode argument to `fopen()` on all platforms — or add a `+` if you want to both read and write on the stream. It works correctly on Unix platforms (even though the `b` is not crucial there); it also works correctly on non-Unix platforms (Windows), and there the `b` is not decoration but is crucial. So, the `b` is harmless on Unix and important on Windows. If your code must work on Windows, use the `b` in the `fopen()` mode. – Jonathan Leffler Jun 09 '21 at 14:09
  • 1
    Do the reading code and writing code share exactly the same definition of `struct employeeRec`? – Ian Abbott Jun 09 '21 at 14:13
  • 1
    @WeatherVane: Thanks for the information. I've updated my answer in the [Using `fflush(stdin)`](https://stackoverflow.com/q/2979209/15168) question to include the extra information you've just provided. – Jonathan Leffler Jun 09 '21 at 14:13

0 Answers0