//readin.c --- fopen/fgets --- copyLeft by tsaiwn@csie.nctu.edu.tw #include #include char FNAME[ ] = "abc.txt"; int main( ) { FILE* fp; static char buf[99]; 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); while( !feof(fp) ) { ++n; x = atof(buf); ans += x; printf("%f ", x); fgets(buf, sizeof(buf), fp); } printf("\nTotal %d items. Sum = %f\n", n, ans); return 0; }