这是我的代码:
fstream f;
f.open("memory.txt", ios::out|ios::in);//'memory.txt' already exists - I open it
//...(here are some operations performed on file f)...
f.close();
remove("memory.txt");
// *** Next, I create a file with the same name ***
fstream f1("memory.txt");
f1.open("memory.txt", ios::in |ios::out |ios::trunc);//#
//...(some operations performed on file f1)...
f1.close();
我的问题是:
f1.open("memory.txt", ios::in |ios::out );
文件 f1 将无法打开。为什么?f1.open("memory.txt", ios::in |ios::trunc );
文件 f1 将无法打开。为什么?f1.open("memory.txt", ios::out |ios::trunc );
,则该文件将会打开!为什么?答案 0 :(得分:1)
ios::out
必须设置ref. ios::trunc
标志会删除所有内容,因此无论文件是否存在,它都会打开。