Test
To find the behavior of getline() in a collision with EOF, I wrote the following test:
int main (int argc, char *argv[]) { size_t max = 100; char *buf = malloc(sizeof(char) * 100); size_t len = getline(&buf, &max, stdin); printf("length %zu: %s", len, buf); }
And input1:
a b c Ctrl-D Enter
Result:
length 4: abc //notice that '\n' is also taken into consideration and printed
Input2:
a b c Enter
Exactly the same conclusion:
length 4: abc
EOF seems to be missing getline()
Source
So, I found the source code of getline() , and the following is the fragment associated with it (and I leave comments and the codes are irrelevant for brevity):
while ((c = getc (stream)) != EOF) { /* Push the result in the line. */ (*lineptr)[indx++] = c; /* Bail out. */ if (c == delim) //delim here is '\n' break; } /* Make room for the null character. */ if (indx >= *n) { *lineptr = realloc (*lineptr, *n + line_size); if (*lineptr == NULL) return -1; *n += line_size; } /* Null terminate the buffer. */ (*lineptr)[indx++] = 0; return (c == EOF && (indx - 1) == 0) ? -1 : indx - 1;
Question
So my question is:
- why the length here is 4 (as far as I can see it should be 5) (as the wiki says: t be EOF if it is not at the beginning of the line)
A similar question: the behavior of EOF when it is accompanied by other values , but pay attention to getline () in this question is different from GNU-getline
I am using GCC: (Ubuntu 4.8.2-19ubuntu1) 4.8.2
source share