scanf/fscanf return 0 at end of file

Felix Lee flee at shire.cs.psu.edu
Tue Jul 25 05:10:17 AEST 1989


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 <<XXXXXX
#include <stdio.h>

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.



More information about the Comp.sys.sun mailing list