p307

源程序:

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream inFile("c:\\tmp\\test.txt", ios::in); //声明对象inFile并调用构造函数
if (inFile)
{
cout << "成功打开文件:c:\\tmp\\test.txt\n";
inFile.close();
}
else
cout << "打开文件失败:c:\\tmp\\test.xt\n";
ofstream outFile("test2.txt", ios::out); //声明对象outFile并调用构造函数
if (!outFile)
cout << "error1" << endl;
else
{
cout << "成功打开文件:test1.txt\n";
outFile.close();
}
fstream outFile2("tmp\\test2.txt", ios::out | ios::in); //声明对象outFile2并调用构造函数
if (outFile)
{
cout << "成功打开文件:tmp\\test2.txt\n";
outFile.close();
}
else
cout << "error2" << endl;
system("pause");
return 0;
}

运行结果:

 

posted @ 2020-02-05 13:02  bobo哥  阅读(131)  评论(0编辑  收藏  举报