Answer the question
In order to leave comments, you need to log in
In what case will the "end of file" be reached when reading data from the keyboard?
There is such a function for reading a string. The function inside is used fgets()
. This function returns NULL
if an error has occurred or the end of the file has been reached .
Under what conditions is "end of file" detected when reading from the keyboard?
char * s_gets(char * str, int n)
{
char * return_value;
int i = 0;
return_value = fgets(str, n, stdin);
if (return_value)
{
while (str[i] != '\n' && str[i] != '\0')
i++;
if (str[i] == '\n')
str[i] = '\0';
else
while (getchar() != '\n')
continue;
}
return return_value;
}
Answer the question
In order to leave comments, you need to log in
The standard end-of-file sign is Ctrl+Z. On Windows, you also need to press Enter or Ctrl+M.
If you have linux, then it will read up to the moment when you pressed enter, then it will return NULL. If you want to read long text, then use a loop like this:
Then your text will be read in bursts of length n.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question