Reputation: 1414
I would like to use EOF to terminate reading bulk user input, but continue to be able to accept user CLI input.
It seems that Linux allows this behaviour, but OS/X simply shuts down the stdin input steam (as seem from the following test program).
Is there any workaround to allow this to work on OS/X?
Sample output from running on MAC OS/X. Once the EOF is detected, no input can be taken. (Lines begin with >>> are output from the program).
TDWONG-M-V08D:$ ./eof_stdin
>>> fgets: zzz
>>> ans=zzz
>>> read stdin again, fgets:
zzz
>>> ans=zzz
TDWONG-M-V08D:$ ./eof_stdin
>>> fgets: >>> ans=
>>> feof(stdin) is true -- EOF detected
>>> read stdin again, fgets:
>>> ans=
Sample output from running on Linux. Input is accepted even after the EOF is detected. (Lines begin with >>> are output from the program).
[tdwong@tdwong-ubuntu tmp]$ ./eof_stdin
>>> fgets: zzz
>>> ans=zzz
>>> read stdin again, fgets:
zzz
>>> ans=zzz
[tdwong@tdwong-ubuntu tmp]$ ./eof_stdin
>>> fgets: >>> ans=
>>> feof(stdin) is true -- EOF detected
>>> read stdin again, fgets:
zzz
>>> ans=zzz
Here is the sample test program:
#include<stdio.h>
int main(void)
{
char ans[80];
//
// read from stdin
printf(">>> fgets: "); fflush(stdin);
fgets(ans, sizeof(ans), stdin);
printf(">>> ans=%s\n", ans);
if (feof(stdin)) {
printf(">>> feof(stdin) is true -- EOF detected\n");
}
//
// continue read from stdin
printf(">>> read stdin again, fgets: \n");
fgets(ans, sizeof(ans), stdin);
printf(">>> ans=%s\n", ans);
return 0;
}
Upvotes: 0
Views: 471
Reputation: 53000
After detecting EOF you can call the stdio function clearerr(stdin)
, or clearerr_unlocked(stdin)
, to clear the EOF status on the stdin
stream - the man 3 clearerr
for more details.
HTH
Upvotes: 1