在此代码中,fout是一个ofstream对象,它假定写入一个名为output.txt
的文件。为什么output.txt
总是空的!我想请教一下这个错误我在代码中所做的:Ofstream不能正常工作?
#include<iostream>
#include<fstream>
#include<stdio.h>//to pause console screen
using namespace std;
double Volume(double);
int main() {
ifstream fin; //read object
ofstream fout;// writing object
double Radius;
fin.open("input.txt");
fout.open("output.txt");
if(!fin){
cout<<"There a problem, input.txt can not be reached"<<endl;
}
else{
fin>>Radius;
fout<<Volume(Radius);
cout<<"Done! check output.txt file to see the sphare volume"<<endl;
}
getchar();//to pause console screen
return 0;
}
double Volume(double r){
double vol;
vol= (4.0/3.0)*3.14*r*r*r;
return vol;
}
缓冲。在调用'getchar()'之前调用'fout.flush()'来刷新它,并且你很好。 – 2011-11-30 20:48:44