fprintf function is running fine but a consequent fscanf is not. It runs into infinite loop at while statement with fscanf not equal to EOF and with NULL only one record when more than one record have been entered through fprintf.
Request to precept it and provide solution
Regards and Thanks
I have attached fscanf() file
fscanf(fp, "%d%s%s%f", &s1.id_num, s1.nm, s1.dep.nm, $s1.salary) != NULL, with
fscanf(fp, "%d%s%s%f", &s1.id_num, s1.nm, s1.dep.nm, $s1.salary) != EOF.
Read the documentation of
fscanf() function which says:
fscanf() function shall read from the named input
stream . The
scanf() function shall read from the standard input stream
stdin . The
sscanf() function shall read from the string
s . Each function reads bytes, interprets them according to a format, and stores the results in its arguments. Each expects, as arguments, a control string
format described below, and a set of
pointer arguments indicating where the converted input should be stored. The result is undefined if there are insufficient arguments for the format. If the format is exhausted while arguments remain, the excess arguments shall be evaluated but otherwise ignored.
fscanf() belongs to an Xscanf() family of functions:
Upon successful completion, these functions shall return the number of successfully matched and assigned input items ; this number can be zero in the event of an early matching failure. If the input ends before the first matching failure or conversion, EOF shall be returned. If a read error occurs, the error indicator for the stream is set, EOF shall be returned, and errno shall be set to indicate the error.
You can see that
fscanf() function never returns
NULL so change the line as I mentioned above.
ok sir accepted
but then it gives infinite loop in case EOF is used.
I have tried the program for more than a week. If you can provide some light on why would fscanf go in infinite loop on using EOF. It would be of great help.
And thanks for replying. I am great full to you. Respects
Share your actual code (with good formatting) with the file you are reading from i.e. demo.txt.