本文转载自:http://blog.163.com/hbu_lijian/blog/static/126129153201201710456994/
C++标准IO类型在三个独立的头文件中定义:iosstream定义读写控制窗口的类型,fstream定义读写已命名文件的类型,sstream多定义的类型则用于读写存储在内存中的string对象。下图为继承关系图:
扩展了wchar_t类型,占2个字节,相应的类为:wiostream,wostream,wistream等等,相应的标准输入输出对象,wcin,wcout等。每个IO头文件都定义了char和wchar_t类型。
强制刷新:endl,flush,ends(用的较少),unitbuf操作符, 自动刷新:程序结束,缓冲区已满 将输入输出绑在一起:如果一个流调用tie函数将其本身绑在传递给tie的ostream实参对象上,则该流上的任何IO操作都会刷新实参所关联的缓冲区。
ifstream:由istream派生而来,提供读文件的功能 ofstream:由ostream派生而来,提供写文件的功能 fstream:有iostream派生而来,提供读写同一个文件的功能
istringstream:由istream派生而来,提供读string的功能 ostringstream:由ostream派生而来,提供写string的功能 stringstream:由iostream派生而来,提供读写string的功能
1.io对象不可复制和赋值的。所以只能引用。
2.它有一些错误的标志,如strm::eofbit.可以通过s.eof()获取到01.这个以后要是有用,再细看吧。
3.cout<<”hell,I’m countryhu!”<
1.
ifstream infile; infile.open(path.c_str()); 参数需转换为c类型。 if(!infile) //如果没有打开成功文件2.ofstream outfile
outfile.open(path.c_str(), ofstream::app); 第二个参数默认为out,清空文件准备写。app为在文件问追加。
3.最好在打开一文件前,in.close();in.clear(); 关闭可能张打开的文件,和清空错误标志;
1.string line,word;
while(getline(cin, line))
{
istringstream string_stream(line); while(string_stream >> word) cout<<word<<endl;}
1.
stringstream word_stream("countryhu!"); cout << word_stream.str()<<endl; word_stream.str("hi,countryhu!");2.流读取一个字符stream.get()
