Home > Back-end >  Within a while loop within the fgets input EOF function why can't stop
Within a while loop within the fgets input EOF function why can't stop

Time:10-03

Probably as shown in the figure below, repeated input CTRL + z, circulation structure and can't jump out as scheduled:

While (the fgets (STR., 10000, stdin)! EOF)={
.
}

Consult everybody a great god, and how to solve! (topic generally need to hit the enter input a string of input, the eof the end of the input, so consider using the fgets input from the keyboard)

CodePudding user response:

This is part of the function
# include & lt; Stdio. H>
# include & lt; stdlib.h>

Struct Record {
Char fname [100].
Char lname [100].
Char nname [100].
Char age [100].
Char gender [100].
Char SHL [100].
Char dep [100].
} record [10000];

Int main ()
{
Char STR [10000].
Int z=0;
While (the fgets (STR., 10000, stdin)! EOF)={

Int len=strlen (STR);
Int x=0; Int y;



//fname
y=0;
For (x=0; xIf (STR [x]! {
=', ')Record [z]. Fname [y]=STR [x];
Y++;
}
The else {
break;
y=0;
}
}
X++;

Printf (" \ n x: % d ", x);
Printf (" \ n fname is right ");
Printf (" record [z]. Fname: % s \ n ", record [z]. Fname);


Z++;
}

CodePudding user response:

The fgets (STR, 10000, stdin)!=NULL
Pay attention to the fgets return values

CodePudding user response:

Check function with
The fgets failure is not returns EOF
https://zh.cppreference.com/w/c/io/fgets

CodePudding user response:

If successful, this function returns the same STR parameter, if you reach the end of the file or read any characters, STR content remains the same, and returns a NULL pointer, if an error occurs, returns a NULL pointer, when reading characters end - of - file, the eof indicator is set, if you haven't read any character encountered this kind of situation, the stream to keep the original content, return NULL; If read error, the error indicator is set, returns NULL, the value of the stream may be changed, "
  • Related