IO流C++
阿新 • • 發佈:2019-01-25
空格 形參 back ring image close 每次 file 文件流
1.iostream處理控制臺IO
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 istream& Test(istream &in) { //IO對象沒有拷貝或者賦值,所以形參和返回值都不能設置為流類型,通常用引用來傳遞流對象。 5 string word; 6 while (in >> word && !in.eof()) { 7 cout << word << endl;View Code8 } 9 in.clear(); 10 return in; 11 } 12 13 void TestCin(istream &in) { 14 int a = 0; 15 auto old_state = cin.rdstate(); //記住cin的當前狀態 16 cin.clear(); //無參數clear,將所有條件狀態復位 17 while (cin >> a) {} 18 cin.setstate(old_state); //將cin設置為原有狀態 19 20 21 //只復位failbit和badbit位,其他不變22 cin.clear(cin.rdstate() & ~cin.failbit & ~cin.badbit); 23 24 } 25 int main(void) { 26 cout << "a" << endl;//加入回車後立刻刷新 27 cout << "a" << ends;//加入空格後立刻刷新 28 cout << "a" << flush;//什麽也不做立刻刷新 29 30 cout << unitbuf;//所以輸出都立刻刷新,無緩沖 31 cout << "a"; 32 cout << nounitbuf;//回到正常的刷新方式 33 cout << "a"; 34 return 0; 35 }
2.fstream處理命名文件IO
1 #include<iostream> 2 #include<fstream> 3 #include<string> 4 #include<vector> 5 using namespace std; 6 vector<string> str; 7 vector<string>::iterator it; 8 string tmp; 9 void printline(string ifile) { 10 ofstream out; //未指定文件打開模式 11 string word; 12 out.open(ifile + ".txt", ofstream::app); //設置文件打開模式為app追加模式 13 while (getline(cin,word) && word != "###") //讀入一行 14 out << word<<endl; //寫入文件 15 out.close();//關閉文件流 16 } 17 void printone(string ifile) { 18 ofstream out; //未指定文件打開模式 19 string word; 20 out.open(ifile + ".txt", ofstream::app); //設置文件打開模式為app追加模式,實際上隱含了out模式,僅以out模式打開文件會丟棄原有數據 21 //上述效果等價於out.open(ifile+".txt),ofstream::out | ofstream::app); 22 while (cin >> word && word != "###") //讀入一個 23 out << word << endl; //寫入文件 24 out.close(); //關閉文件流 25 } 26 void readone(string ifile) {//將文件內容讀入到一個string的vector容器中 27 str.clear(); 28 ifstream openfile(ifile+".txt",ifstream::in); //以讀模式打開一個文件 29 while(openfile >> tmp) { //沒有到達文件的尾部 30 //讀入一個 31 str.push_back(tmp); //每個元素單獨存入vector中 32 } 33 if (str.empty()) { //沒有數據,直接返回 34 cout << "No data?!" << endl; 35 return ; 36 } 37 it = str.begin(); 38 for (; it != str.end(); it++) //輸出文件內容(存入vector中) 39 cout << (*it) << endl; 40 openfile.close(); //關閉文件流 41 42 } 43 void readline(string ifile) {//將文件內容讀入到一個string的vector容器中去 44 str.clear(); 45 ifstream openfile(ifile + ".txt", ifstream::in); //以讀模式打開一個文件 46 while (getline(openfile, tmp)) { //沒有到達文件的尾部 47 //讀入一行 48 str.push_back(tmp); //每一行作為獨立元素存入vector中 49 } 50 if (str.empty()) { //沒有數據,直接返回 51 cout << "No data?!" << endl; 52 return; 53 } 54 it = str.begin(); 55 for (; it != str.end(); it++) //輸出文件內容(存入vector中) 56 cout << (*it) << endl; 57 openfile.close(); //關閉文件流 58 } 59 int main(void) { 60 printone("1"); 61 readone("1"); 62 printline("1"); 63 readline("1"); 64 return 0; 65 }View Code
- 讀入過程
-
- 文件寫入後的txt
-
3.stringstream完成內存string的IO
1 #include<iostream> 2 #include<sstream> 3 #include<string> 4 #include<vector> 5 using namespace std; 6 7 int main(void) { 8 ostringstream os; 9 os << "dad"; 10 string as = os.str(); 11 string bs = "1213213"; 12 os << bs; 13 cout << as << endl; 14 as= os.str(); 15 cout << as << endl; 16 17 //如果構造的時候設置了字符串參數,那麽增長操作的時候不會從結尾開始增加,而是修改原有數據,超出的部分增長 18 ostringstream temp("1"); 19 cout << temp.str() << endl; 20 temp << "dsadas"; //修改原有數據後追加字符串 21 string t1 = temp.str(); 22 cout << t1 << endl; 23 24 25 os.clear();//如果需要使用同一個流,每次使用之前都需要clear一下 26 int a = 15216; 27 stringstream ans;//int轉字符串 28 string b; 29 ans << a; 30 ans >> b; 31 cout << b << endl; 32 return 0; 33 }View Code
4.總結
類fstream和stringstream都是繼承類iostream的,輸入繼承istream,輸出繼承ostream,所以能都使用istream的地方都可以使用ifstream和istringstream,對ostream同理。
IO流C++