//readin.c --- fopen/fgets --- copyLeft by tsaiwn@csie.nctu.edu.tw #include #include char FNAME[ ] = "abc.txt"; char*p; int len; int main( ) { FILE* fp; static char buf[9]; double ans, x; int n; fp = fopen(FNAME, "rt"); // open for read, text file if(fp == 0) { fprintf(stderr, "open file %s ", FNAME); perror("Error "); return 38; } ans = n = 0; //fgets(buf, sizeof(buf), fp); fpurge(fp); p = fgetln(fp, &len); printf(" len=%d\n", len); while( !feof(fp) ) { ++n; x = atof(p); ans += x; printf("%f ", x); //fgets(buf, sizeof(buf), fp); p = fgetln(fp, &len); } printf("\nTotal %d items. Sum = %f\n", n, ans); return 0; }