c - Using fscanf to check if more lines exist -
i have function read numbers txt files structured so:
1 2 5 2 1 9 3 5 8
the function reads values correctly values, want check if line have read last in file.
my last if statement in below function attempts seeing if fscanf produces null doesn't work, function returns null if it's not last line.
void process(int linenum, char *fullname) { int ii, num1, num2, num3; file* f; f = fopen(fullname, "r"); if(f==null) { printf("error: not open %s", fullname); } else { (ii=0 (ii = 0; ii < (linenum-1); ii++) { /*move through lines without scanning*/ fscanf(f, "%d %d %d", &num1, &num2, &num3); } if (fscanf(f, "%*d %*d %*d\n")==null) { printf("no more lines"); } fclose(f); } }
you can use feof()
check if reading past end of file.
from man page of fscanf
:
return value these functions return number of input items matched , assigned, can fewer provided for, or 0 in event of matching failure.
you if last line trying read not in expected format, fscanf
may not read , return 0
same null
.
Comments
Post a Comment