.profile
fgets(3)
gets(3)
io1.c
/* Demonstrates the equivalence of stream input and output. */ #include <errno.h> #include <string.h> #include <stdio.h> #define MAXBUF 256 /* Compile: cc -g -ansi -pedantic -Wall -O2 -o io1 io1.c Run: ./io1 */ /* Prototypes. */ int flush(); /* Input a line, then immediately output it. */ int main() { char *cptr = NULL; char buffer[MAXBUF]; /* OUTPUT: fputs() returns a non-negative integer if okay and EOF on error. */ if ((fputs("Hello world!\n", stdout)) == EOF) { if (errno) fprintf(stderr, "fputs() error: %s\n", strerror(errno)); return 1; } printf("Enter some stuff:\n"); /* Flush output. */ if (flush()) return(1); /* INPUT: fgets() returns 'buffer' if everything works fine, or NULL and errno on error. */ cptr = fgets(buffer, MAXBUF, stdin); if (cptr == NULL) { if (errno) { fprintf(stderr, "fputs() error: %s\n", strerror(errno)); return 1; } } else { printf("You typed : %s", cptr); } /* Flush output. */ if (flush()) return(1); /* INPUT and OUTPUT on the same line: no error checking, and thus NOT recommended. */ printf("Enter some stuff (no error checking):\n"); if (flush()) return(1); printf("You typed (no error checking): "); fputs(fgets(buffer, MAXBUF, stdin), stdout); return 0; } int flush() { if ((fflush(stdout)) != 0) { if (errno) fprintf(stderr, "fflush() error: %s\n", strerror(errno)); return 1; } return 0; }