Ofstream 效果不好?

发布于 2024-12-18 15:20:17 字数 821 浏览 3 评论 0原文

在此代码中,fout 是一个 ofstream 对象,它假设写入一个名为 output.txt 的文件。由于output.txt总是空的!。我想问一下我在代码中犯的错误:

#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;
}

In this code, fout is an ofstream object, It supposes to write to a file called output.txt. For a reason output.txt always empty!. I would ask about the mistake I did in the code :

#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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

写下不归期 2024-12-25 15:20:17

“output.txt 始终为空”

我怀疑您不允许 fout 刷新其输出。这些陈述中的任何一个适用于您吗?

  • getchar()之后检查“output.txt”的内容
    调用,但在程序结束之前?

  • 你用Ctrl+C结束程序?

如果是这样,则您不允许将数据写入fout。您可以通过避免这两种情况或执行以下操作之一来解决此问题:

  • Add fout << endl 写入数据后,或

  • 添加 fout <<写入数据后刷新,或

  • 写入数据后

    添加fout.close()

"output.txt always empty"

I suspect that you are not allowing fout to flush its output. Do either of these statements apply to you?

  • You check the contents of "output.txt" after the getchar() is
    invoked, but before the program ends?

  • You end the program with a Ctrl+C?

If so, you have not allowed the data to be written to fout. You can solve this problem by avoiding those two conditions, or doing one of these:

  • Add fout << endl after you write your data, or

  • Add fout << flush after you write your data, or

  • Add fout.close() after you write your data.

中二柚 2024-12-25 15:20:17

你必须刷新流,在完成输出后调用fout.flush(),你所做的就是构建一个尚未写入文件的缓冲区。 flush 实际上将缓冲区放入文件中。

You have to flush the stream, call fout.flush() after you are done outputting, all you are doing is building a buffer that has yet to be written to the file. flush actually puts the buffer into the file.

愛上了 2024-12-25 15:20:17

除了调用 fout.flush() 之外,您还可以将: 更改

fout<<Volume(Radius);

fout<<Volume(Radius) << std::endl; // Writes a newline and flushes.

: 或者如果不再需要,也可以关闭流 fout.close()

In addition to calling fout.flush() you could change:

fout<<Volume(Radius);

to

fout<<Volume(Radius) << std::endl; // Writes a newline and flushes.

or you can close the stream fout.close() if it is no longer required.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文