[英]Ofstream doesn't seem to be outputting
所以我试图为计算器创建一个日志,这样我就可以回去检查以确保所有输入的数字都正确输入。 这是代码。
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream f;
f.open("pumpout.txt");
float number = 0;
float total = 0;
char operand;
bool running = true;
cin >> total;
f << total << " ";
cin >> operand;
f << operand << " ";
cin >> number;
f << number << " = ";
while (running = true) {
if (operand == '/') {
total = total / number;
cout << total << endl;
f << total << "\n" << total << " ";
cin >> operand;
}
else if (operand == '*') {
total = total * number;
cout << total << endl;
f << total << "\n" << total << " ";
cin >> operand;
}
else if (operand == '+') {
total = total + number;
cout << total << endl;
f << total << "\n" << total << " ";
cin >> operand;
}
else if (operand == '-') {
total = total - number;
cout << total << endl;
f << total << "\n" << total << " ";
cin >> operand;
}
f << operand << " ";
cin >> number;
f << number << " = ";
}
}
所以它按照我想要的方式添加并执行所有操作,但它不会转到文本文件。 格式应该是:
total operand # = total
通过整个文本文件。 任何帮助都会很棒。
我希望您看不到任何输出,因为您的循环永远不会停止并且您的文件永远不会关闭。 如果您希望输出立即出现在文件中,您应该使用std::flush
或std::endl
刷新文件,例如f << total << "\\n" << total << " " << flush;
.
出于效率原因 file output is buffered ,这意味着文件输出首先写入缓冲区,它不会立即出现在文件中。 刷新是一个获取缓冲区并立即将其写入文件的过程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.