D
D
dandropov952018-08-25 22:27:36
C++ / C#
dandropov95, 2018-08-25 22:27:36

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 NULLif 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

2 answer(s)
R
Rsa97, 2018-08-25
@dandropov95

The standard end-of-file sign is Ctrl+Z. On Windows, you also need to press Enter or Ctrl+M.

I
Ivan Sharapenkov, 2018-08-25
@sM0kfyz

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 question

Ask a Question

731 491 924 answers to any question