C++ Primer(第五版) 第八章:IO庫
阿新 • • 發佈:2018-09-01
cor 三種 cer record info pre hide std 2-0
練習8.1:考察如何管理流的狀態
1 istream& func(istream& is) 2 { 3 string buf; 4 while ( is>>buf ) cout<<buf<<endl; 5 is.clear(); 6 return is; 7 }練習8.1
練習8.2:考察同上
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 5 istream& func(istream& is練習8.2) 6 { 7 string buf; 8 while ( is>>buf ) cout<<buf<<endl; 9 is.clear(); 10 return is; 11 } 12 13 int main() 14 { 15 istream& is=func(cin); 16 cout<<is.rdstate()<<endl; 17 return 0; 18 }
練習8.3:考察cin的錯誤狀態
輸入使得cin進入了錯誤狀態會導致循環終止。如:badbit,failbit,eofbit
練習8.4:考察文件流的使用
1 #include<iostream> 2 #include<fstream> 3 #include<vector> 4 #include<string> 5 using namespace std; 6 7 void ReadFileToVec(const string& fileName,vector<string>& vec) 8 { 9 ifstream ifs(fileName); 10 if ( ifs ) 11 { 12 string練習8.4buf; 13 while ( getline(ifs,buf) ) vec.push_back(buf); 14 } 15 } 16 17 int main() 18 { 19 vector<string>vec; 20 ReadFileToVec("../date/book.txt",vec); 21 for ( const auto& i:vec ) cout<<i<<endl; 22 return 0; 23 }
練習8.5:考察同上
1 #include<iostream> 2 #include<fstream> 3 #include<vector> 4 #include<string> 5 using namespace std; 6 7 void ReadFileToVec(const string& fileName,vector<string>& vec) 8 { 9 ifstream ifs(fileName); 10 if ( ifs ) 11 { 12 string buf; 13 while ( ifs>>buf ) vec.push_back(buf); 14 } 15 } 16 17 int main() 18 { 19 vector<string>vec; 20 ReadFileToVec("../date/book.txt",vec); 21 for ( const auto& i:vec ) cout<<i<<endl; 22 return 0; 23 }練習8.5
練習8.9:考察istringstream的使用
1 #include<iostream> 2 #include<string> 3 #include<sstream> 4 using namespace std; 5 6 istream& func(istream& is) 7 { 8 string buf; 9 while ( is>>buf ) cout<<buf<<endl; 10 is.clear(); 11 return is; 12 } 13 14 int main() 15 { 16 istringstream iss("hello world"); 17 func(iss); 18 return 0; 19 }練習8.9
練習8.10:考察同上
1 #include<iostream> 2 #include<vector> 3 #include<string> 4 #include<fstream> 5 #include<sstream> 6 using namespace std; 7 8 void ReadFileToVec(const string& fileName,vector<string>& vec) 9 { 10 ifstream ifs(fileName); 11 if ( ifs ) 12 { 13 string line; 14 while ( getline(ifs,line) ) vec.push_back(line); 15 } 16 } 17 18 int main() 19 { 20 vector<string>vec; 21 ReadFileToVec("../date/book.txt",vec); 22 for ( auto& s:vec ) 23 { 24 istringstream iss(s); 25 string word; 26 while ( iss>>word ) cout<<word<<endl; 27 } 28 return 0; 29 }練習8.10
練習8.11:考察同上
在每次使用時需要先清楚原有內容,在重新拷貝
1 #include<iostream> 2 #include<sstream> 3 #include<string> 4 #include<vector> 5 using namespace std; 6 struct PersonInfo{ 7 string name; 8 vector<string>phones; 9 }; 10 11 int main() 12 { 13 string line,word; 14 vector<PersonInfo>people; 15 istringstream record; 16 while ( getline(cin,line) ) 17 { 18 PersonInfo info; 19 record.clear(); 20 record.str(line); 21 record>>info.name; 22 while ( record>>word ) info.phones.push_back(word); 23 people.push_back(info); 24 } 25 }練習8.11
練習8.12:
參考網上答案:因為這裏我們需要聚合類,所以我們不需要使用類內初始化
練習8.13:三種流的綜合應用
1 #include<iostream> 2 #include<sstream> 3 #include<string> 4 #include<vector> 5 #include<fstream> 6 using namespace std; 7 struct PersonInfo{ 8 string name; 9 vector<string>phones; 10 }; 11 12 bool valid(const string& s) 13 { 14 return isdigit(s[0]); 15 } 16 17 string format(const string& str) 18 { 19 return str.substr(0,3)+"-"+str.substr(3,3)+"-"+str.substr(6); 20 } 21 22 int main() 23 { 24 ifstream ifs("../date/phonenumbers.txt"); 25 if ( !ifs ) 26 { 27 cerr<<"no phone numbers?"<<endl; 28 return -1; 29 } 30 string line,word; 31 vector<PersonInfo>people; 32 istringstream record; 33 while ( getline(ifs,line) ) 34 { 35 PersonInfo info; 36 record.clear(); 37 record.str(line); 38 record>>info.name; 39 while ( record>>word ) info.phones.push_back(word); 40 people.push_back(info); 41 } 42 for ( const auto& entry:people ) 43 { 44 ostringstream formatted,badNums; 45 for ( const auto& nums:entry.phones ) 46 { 47 if ( !valid(nums) ) badNums<<" "<<nums; 48 else formatted<<" "<<format(nums); 49 } 50 if ( badNums.str().empty() ) 51 { 52 cout<<entry.name<<" "<<formatted.str()<<endl; 53 } 54 else 55 { 56 cerr<<"input error: "<<entry.name<<"invalid number(s)"<<badNums.str()<<endl; 57 } 58 } 59 }練習8.13
練習8.14:
因為它們都是類不是內置類型,所以使用引用會更加高效
又因為它們在整個過程中並不需要修改,所以使用const
C++ Primer(第五版) 第八章:IO庫