C++中由ofstream导入的txt文件如用cout打印出来?

2024-12-20 20:00:22
推荐回答(2个)
回答1:

比如说你的已经打开了一个"my.txt"的文件
ifstream infile("my.txt".ios::in);
if(!infile)
{
cerr<<"文件打开失败"< return;
}
char ch;
while(!infile.eof())
{
infile >> ch;
cout <}

回答2:

#include
#include
using namespace std;
void main()
{
float x,y,z;
int k;
ifstream indata;
ofstream outdata;
indata.open("in.dat");
if(!indata)
{
cout<<"can not open the file:in.dat"< return;
}
indata>>x>>y>>z;
indata>>k;
cout< outdata.open("out.dat");
if(!outdata)
{
cout<<"can not open the file :out.dat"< return;
}
outdata< outdata< indata.close();
outdata.close();
}