下面是我不完整的程序。我在写入文本文件时遇到问题。例如,我想将下雪天数写入一个文本文件,但当我在 VS 2010 中调试时,文本文件中没有显示任何内容。它确实显示了我的信息和姓名,但没有其他任何作用。之后它不会写任何东西。它不写入文本文件。
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
const string INFORMATION = "College Class";
const string MY_NAME = "Tom Hangler";
int main(void)
{
ofstream outFile;
int numberOfSnowDays;
int greatestSnowDay;
int dayNumber;
double amounttOfSnow;
outFile.open("Ex1Out.txt");
outFile << setw(51) << INFORMATION << endl << setw(48) << MY_NAME << endl;
cout << "Please enter num of days it snowed: " << endl;
cin >> numberOfSnowDays;
outFile << setw(10) << "Number of days of snow is: " << setw(10) << numberOfSnowDays;
int index;
//Problem 1 for-loop
for (index = 0; index < numberOfSnowDays; index++)
{
cout << "Enter day: " << endl;
cin >> dayNumber;
cout << "Enter amount of snow: " << endl;
cin >> amountOfSnow;
};
return 0;
}
这是我的输出显示: 大学类(class)(居中) 汤姆汉格勒(居中)
如果我尝试在这之后写任何东西,输出文件中不会写入任何内容。输出文本文件在我的 VS 项目中,其中包含我的 .cpp 文件。我将文本文件添加到项目中。
最佳答案
尝试 closing函数末尾的流,看起来数据没有被刷新。
outFile.close();
关于c++ - outFile C++ 不写入输出文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9181299/