question

JitendraKumarChauhan-1166 avatar image
0 Votes"
JitendraKumarChauhan-1166 asked JitendraKumarChauhan-1166 edited

Getting extra characters while reading records from a file using structure.

I wrote a C program to read records from a file using structure but when i compiled it i got few extra characters apart from those which are written in my file "EMPLOYEE". So Please resolve my issue. Screenshot of the file content and debug console has been attached herewith.103805-debug-console-screenshot.png103806-file-content-screenshot.png
Here is the C Program---

include<stdio.h>

include<conio.h>

include<stdlib.h>

int main()
{
FILE* fp;
errno_t err;
char another = 'Y';
struct emp
{
char name[31];
int age;
float bs;
};
struct emp e;
err = fopen_s(&fp, "EMPLOYEE.DAT", "r");
if (err != 0)
{
puts("Can Not Open file");
exit(1);
}
else
while ((fscanf_s(fp,"%s%d%f",e.name, sizeof(e.name), &e.age, &e.bs)) != EOF)
printf("%s%d%f\n", e.name, e.age, e.bs);
fclose(fp);
return 0;
}


c++
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

If you step through your program in the debugger, you can examine the variables and discover the cause of the problem - it's fun to find things out.

1 Vote 1 ·

Which values do you expect in e.name, e.age and e.bs for each row? Maybe fscanf_s can be adjusted.



0 Votes 0 ·
RLWA32-6355 avatar image
0 Votes"
RLWA32-6355 answered JitendraKumarChauhan-1166 edited

Assuming that the first 2 characters are name, the next two characters are age and the remainder of the line is the bs float try this -

         while ((fscanf_s(fp, "%2s%2d%f", e.name,  (unsigned int) _countof(e.name), &e.age, &e.bs)) != EOF)
             printf("%s %d %f\n", e.name, e.age, e.bs);
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Yes right, It is working. Thanks.

0 Votes 0 ·
DavidLowndes-6766 avatar image DavidLowndes-6766 JitendraKumarChauhan-1166 ·

But have you learned how to debug your code, and diagnose/fix the issue for yourself?

1 Vote 1 ·

Yes I have been learning using breakpoints.

0 Votes 0 ·
RLWA32-6355 avatar image RLWA32-6355 JitendraKumarChauhan-1166 ·

@DavidLowndes-6766 makes an excellent point.

Now that you have a working example you should go back to the documentation for fscanf_s and read it carefully to understand exactly what the suggested solution is doing and why the results differ from your original posted code.

Have you done that?

0 Votes 0 ·
GuidoFranzke avatar image
0 Votes"
GuidoFranzke answered JitendraKumarChauhan-1166 commented

Hello,
did you check the variables with the debugger?
IMO, fscanf is the wrong approach. %s will read the full string "lk2587000.0000" . To separate the values to "lk", 2587000 and 0.0 , you must implement your own string analysis routine.
Regards, Guido
In addition: it is always usefull to initialize the values of a variable/struct/etc.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I tried using %s only. It is working. Thanks

0 Votes 0 ·