c - Detecting EOF with fgets() where filesteam is stdin -


bit of background, i'm writing program plays game "boxes" runs in linux command line , written in c. there's prompt waits user input , read fgets() , interpretted etc.

as part of task specification have return specific error if reach "end of file while waiting user input". understand fgets() returns null when reaches eof... have

fgets(input,max_buffer,stdin);

in prompt loop if user exits prematurely ctrl+c or ctrl+d mean input == null?

can detect when user fgets?

just trying head around this, in advance help.

(os: unix) (compiler: gcc - c90)

from here, fgets:

reads characters stream , stores them c string str until (num-1) characters have been read or either newline or end-of-file reached, whichever happens first.

a newline character makes fgets stop reading, considered valid character function , included in string copied str.

a terminating null character automatically appended after characters copied str.

so, fgets return when user inputs ctrl-d (eof) or, when \n (newline) encountered. ctrl-c by default terminate program entirely.

if you'd catch ctrl-c, , exit gracefully, could:

#include <signal.h>  void inthandler(int dummy) {     //graceful ctrl-c exit code. }  int main(void) {     signal(sigint, inthandler);     //your code } 

Comments

Popular posts from this blog

php - Admin SDK -- get information about the group -

dns - How To Use Custom Nameserver On Free Cloudflare? -

Python Error - TypeError: input expected at most 1 arguments, got 3 -