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