c++ 文件的输入与输出
2010-11-21 11:42
337 查看
(1),ifstream,
ifstream infile(ifile.c_str()); //construct an ifstream and bind it to the file named ifile
或infile.open("in");
(2),ofstream,
ofstream outfile(ofile.c_str()); //ofstream output file object to write file named ofile
或 outfile.open("out");
(3),fstream.
检查文件打开是否成功
if(!infile) {
cerr << "error: unable to open input file : " << ifile << endl;
return -1;
}
如果程序员需要重用文件流读写多个文件,必须在读另一个文件之前调用 clear() 清除该流的状态
一个打开并检查输入文件的程序
// opens in binding to the given file
ifstream& open_file(ifstream &in, const string &file)
{
in.close(); //close in case it was already open
in.clear(); //clear any existing errors
//if the open fails, the stream will be in an invalid state
in.open(file.c_str()); //open the file we were given
return in; //condition state is good if open succeeded
}
ifstream infile(ifile.c_str()); //construct an ifstream and bind it to the file named ifile
或infile.open("in");
(2),ofstream,
ofstream outfile(ofile.c_str()); //ofstream output file object to write file named ofile
或 outfile.open("out");
(3),fstream.
检查文件打开是否成功
if(!infile) {
cerr << "error: unable to open input file : " << ifile << endl;
return -1;
}
如果程序员需要重用文件流读写多个文件,必须在读另一个文件之前调用 clear() 清除该流的状态
一个打开并检查输入文件的程序
// opens in binding to the given file
ifstream& open_file(ifstream &in, const string &file)
{
in.close(); //close in case it was already open
in.clear(); //clear any existing errors
//if the open fails, the stream will be in an invalid state
in.open(file.c_str()); //open the file we were given
return in; //condition state is good if open succeeded
}
相关文章推荐
- C++中的文件输入/输出ios:xx eat Processing(zz)
- C++中的文件输入/输出
- C++简单文件的输入/输出
- C++中的文件输入/输出
- 2012/2/11 《C++ Primer Plus》第十七章:输入、输出和文件 学习笔记
- C++的输入、输出与文件
- C C++ 文件输入与输出
- C/C++-标准输入/输出重定向为文件输入/输出
- C/C++的cmd下的编译运行以及文件输入输出
- freopen - C/C++文件输入输出利器
- 【c++笔记十三】c++中的输入、输出和文件操作
- 【c++笔记十三】c++中的输入、输出和文件操作
- C++中文件的输入输出
- C++的文件输入与输出
- C++中的文件输入/输出:掌握输入/…
- C++ 文件输入/输出(1)
- 转 C++学习笔记 之输入输出和文件
- C++中输入输出文件的格式
- C++——输入、输出和文件
- C++学习之文件的输入输出