Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!iuvax!purdue!decwrl!ucbvax!husc6!rice!sun-spots-request From: flee@shire.cs.psu.edu (Felix Lee) Newsgroups: comp.sys.sun Subject: scanf/fscanf return 0 at end of file Keywords: SunOS Message-ID: <380@brazos.Rice.edu> Date: 24 Jul 89 19:10:17 GMT Sender: root@rice.edu Organization: Sun-Spots Lines: 52 Approved: Sun-Spots@rice.edu Serial Number: 992E0914 Machine Type: Sun 4/280S O/S Version: SunOS 4.0.3 Organization: Computer Science Department, The Pennsylvania State University 333 Whitmore Laboratory, University Park, PA 16802 Phone Number: +1 814 865 9723 Description: scanf and fscanf return zero at end of file instead of EOF which is -1. Repeat-By: %cat > loop.c < FILE *fopen(), *fp; main( argc, argv) int argc; char *argv[]; { long bytes; char dummy; int n; /*fp = fopen(argv[1], "r"); */ bytes = 0; /* while((n=fscanf(fp, "%c", &dummy)) != EOF) { */ while ((n=scanf("%c", &dummy)) != EOF) { fprintf(stdout, "n=%d\n", n); bytes++; } printf("There are %ld bytes in <%s> file.\n", bytes, argv[1]); } XXXXXX % cc -g -O0 -o loop loop.c % loop abc ^D The output should look like: n=1 n=1 n=1 n=1 n=0 n=0 n=0 etc The same result happens if the input is redirectted from a file or if fscanf is used to read a specific file.