I’m having trouble with file scans and are these 2 expressions equivalent to each other in the manner that they operate?
#include <stdio.h>
FILE *point;
int number
while ( fscanf(point, "%d", &number) != -1)
while ( !feof(point) )
(test file):
39203 Thao Nguyen
92039 Steven Gonzales
For some reason the first statement works for me but the second statement just gives me seg faults, because it keeps reading the file when there not left to be read.
I tried a third way
while ( point != EOF) // but this gives me a error of integer to pointer error
EOF is a constant defined in stdio.h usually as -1. That’s the reason that the first one happens to work for you. However, it’s bad practice to use the literal -1 as an implementation can theoretically define EOF however it wants. So you really want to do something along the lines of
while ( fscanf(point, "%d", &number) != EOF )See the fscanf man page:
http://www.kernel.org/doc/man-pages/online/pages/man3/scanf.3.html