11-7.txt
来自「c和c++完美演绎,里面有编程的方法,对编程技术的提高有很大的帮助」· 文本 代码 · 共 27 行
TXT
27 行
/* 范例:11-7 */
#include <fstream.h>
void main()
{
ofstream out_file("out.txt"); // # 1 if(变更out.txt属性只读,隐藏)
out_file << "My name is Peipei.\n";
out_file.put('B').put('o').put('y'); // # 2如同cout.put(ch)...
cout << "out_file.is_open()=" << out_file.is_open() << endl;
out_file.close(); // # 3
ifstream in_file("out.txt"); // # 4 if(变更为不存在文件,out1.txt)
cout << "in_file.is_open()=" << in_file.is_open() << endl; // # 5
cout << "in_file.fail()=" << in_file.fail() << endl; // # 6
cout << in_file.rdbuf(); // # 7
in_file.close(); // # 8
getchar();
}
程序执行结果:(依 #1、#4 设定而不同,两者皆成功时,结果如下)
out_file.is_open()=1
in_file.is_open()=1
in_file.fail()=0
My name is Peipei.
Boy
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?