//readin.cpp --- cin.getline --- copyLeft by tsaiwn@csie.nctu.edu.tw #include #include #include char FNAME[ ] = "abc.txt"; int main( ) { ifstream in(FNAME); // required static char buf[99]; double ans, x; int n; //in.open(FNAME); // open for read, text file if(in.fail( ) ) { cerr << "open file " << FNAME << endl; perror("Error "); return 38; } ans = n = 0; in.getline(buf, sizeof(buf) ); while( !in.eof() ) { ++n; x = atof(buf); ans += x; cout << x << " "; in.getline(buf, sizeof(buf) ); } cout << "\nTotal "<< n << " items. Sum = "; cout << ans << endl; return 0; }