C++ 如何实现文件的保存

来源:百度知道 编辑:UC知道 时间:2024/06/25 01:54:37
这是我们的作业。我的理解是文件写入的时候就自动保存了。
不知道有没有更具体的实现方法:

一旦写入就说明它已存在外存上了啊。

写入就写入了,写入这个过程就相当于记事本啊、Word啊这些保存的这个过程而不是编辑的过程。

所以你有些理解上的问题哦。。。

C++保存文件方法很多啦。
1.兼容C的方法:FILE
2.C++的fstream
3.Windows的API等等。。。

给你这个函数作参考

#include <iostream>
#include <string>
using namespace std;

void save_on_file(string filename){
//存盘
ofstream out_obj;
out_obj.open(filename.c_str());
if(!out_obj){
cout<<"\n open "<<filename<<" failed\n";
return ;
}
out_obj<<"File "<<filename<<" is createed! "<<endl;
out_obj.close();//只有close之后,上面的输出才写进文件中
}
void main(){
string s;
cin>>s;
save_on_file(s);
}