2024年3月6日发(作者:)
1 关于C++中标准IO库中流的概念(istringstream,ifstream,istream等)
istream,ostream用于控制窗口的输入输出,需要头文件#inclujde
ifstream,ofstream用于将文件转化为一个流对象(IO),实现向对控制台一样简单的输入输出,需要头文件#include
istringstream,ostringstream用于将一个字符串(其实是内存中的数据)转化为一个流对
象,实现流的输入输出,需要头文件#include
以上代码在使用之前千万别忘了加上命名空间 using namespace std;否则将会出现许多莫名其妙的错误
For example:
#include
#include
#include
#include
using namespace std;//命名空间不可或缺的一句
//窗口输入输出流
void win_io(istream &input){
//parameter needs a win_stream
}
//文件输入输出流
void file_io(){
string file_name("./"); //文件名,当前目录下名为Smile的txt文件
cout << "文件中的内容为:" << endl;
ifstream in_file(file_name.c_str()); //将流与一个文件绑定,
//file_name.c_str()是将string类型转化为c风string line;
cout << "请任意输入一些字符,按回车结束" << endl;
getline(input,line);
cout << "从控制窗口读取的内容为:" << line < 格字符串,这是历史问题,不必过分追究 string line; } //将内存中的内容转化为流对象 void buf_io(){ string buf("hello world"); istringstream sstr(buf); string word; while(getline(in_file,line)) cout << line << endl; } while(sstr >> word) cout << word << endl;


发布评论