IO流C++

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;
 8     }
 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 }
View Code

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同理。

原文地址:https://www.cnblogs.com/FlyerBird/p/10319119.html