c++文件流的问题 这个程序执行后 为什么 实验.txt 中什么也没有了

来源:百度知道 编辑:UC知道 时间:2024/06/02 16:59:51
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main(){
ifstream in("实验.txt");
ofstream out("实验.txt");
string s;
in>>s;
cout<<s;
system("pause");
}

#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main(){
ifstream in("实验.txt");
ofstream out("实验.txt",fstream::app);
string s;
in>>s;
cout<<s;
in.close();
out.close();
system("pause");
}
用ofstream对象打开一个文件 默认是会清空原有数据的 还有写程序文件记得用完要close啊

在执行
ofstream out("实验.txt");
的时候,"实验.txt"已经被清空了,所以根本读不出数据。
除非你打开时标上"ios::app"之类的

ofstream的默认打开方式是ofstream::out,他的功能是,打开文件,并清空其内容,所以,你的结果是什么也没有