8.1 8.2 /* * c++ primer exercise. * test file. */ #include <iostream> using namespace std; istream & read_write(istream &in) { // char cval; int ival; while(in>>ival) cout<<ival<<' '; in.clear(); return in; } int main(int argc, char* argv[]) { read_write(cin); system("pause"); return 0; } 8.3 读取类型不匹配,EOF,发生系统级不可恢复的状态。 8.4 /* * c++ primer exercise. * test file. */ #include <iostream> #include <fstream> #include <vector> #include <string> using namespace std; int main(int argc, char* argv[]) { string sval; vector<string> svec; ifstream in(*(argv+1)); if(in){ while(getline(in,sval)){ svec.push_back(sval); //cout<<sval<<endl; } }else{ cerr<<"can not open: "+ string(*(argv+1)); return -1; } for(unsigned int i=0;i<svec.size();++i){ cout<<svec[i]<<endl; } system("pause"); return 0; } 8.5 /* * c++ primer exercise. * test file. */ #include <iostream> #include <fstream> #include <vector> #include <string> using namespace std; int main(int argc, char* argv[]) { string sval; vector<string> svec; ifstream in(*(argv+1)); if(in){ while(in>>sval){ svec.push_back(sval); //cout<<sval<<endl; } }else{ cerr<<"can not open: "+ string(*(argv+1)); return -1; } for(unsigned int i=0;i<svec.size();++i){ cout<<svec[i]<<endl; } system("pause"); return 0; }
时间: 2024-10-10 21:27:36