I'm using a modified fgets() function called s_gets() that removes a newline from input or discards any of the remaining characters in the input buffer. It looks like the following;
char *s_gets(char *str, int n, FILE *pf) {
char *ret_val;
char *find;
ret_val = fgets(str, n, pf);
if (ret_val) {
find = strchr(str, '\n');
if (find) {
puts("Newline was found.");
printf("Character before \\n is %c\n", *(find - 1));
*find = '\0';
} else {
while (getchar() != '\n')
continue;
}
}
return ret_val;
}
When I use this function and pass it a FILE*
to a file containing just the string apple
on a single line, the puts()
inside the if clause runs and the printf()
statement prints Character before \n is e
. My question is where is this mysterious newline coming from? Does this have anything to do with EOF? I'm compiling this with Apple LLVM version 10.0.0 (clang-1000.10.44.2) on macOS 10.14.