outFile C ++不写入输出文本 [英] outFile C++ not writing to output text
问题描述
#include< iostream>信息信范范此预读亦范亦方范范范范范辛辛
#include< string>
#include< fstream>
使用namespace std;
const字符串INFORMATION =College Class;信中的内预范读亦读亦信息亦亦范范信息预读
int main(void)
{
ofstream outFile;
int numberOfSnowDays;
int largestSnowDay;
int dayNumber;
double amounttOfSnow;
outFile.open(Ex1Out.txt);
outFile<< (51)<信息< endl<< (48)< MY_NAME<< ENDL;
cout<< 请输入下雪天数:<< ENDL;
cin>> numberOfSnowDays;
outFile<< (10)< 下雪的天数是:<< (10)< numberOfSnowDays;
int index;
//问题1 for循环
for(index = 0; index< numberOfSnowDays; index ++)
{
cout< 输入日期:<< ENDL;
cin>> dayNumber;
cout<< 输入积雪量:<< ENDL;
cin>> amountOfSnow;
};
返回0;
$ b $ p
$ b 这里是我的输出显示:
College Class(居中)
Tom Hangler(居中)
如果我在此之后尝试写入任何内容,则不会将任何内容写入输出文件。输出文本文件是在我的VS项目中包含我的.cpp文件。我添加了文本文件到项目。
解决方案 尝试 outFile.close();
Down below is my incomplete program. I am having problems with writing to a text file. For example I want to write the number of snow days to a text file, but nothing shows up in the textfile when I debug in VS 2010. It does display my info and name, but nothing else works. It wont write anything after that. its NOT writing to a text file.
#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;
}
here is what my output displays:
College Class (centered)
Tom Hangler (centered)
If i try to write anything after this, Nothing is written ever to the output file. And the output text file IS in my VS project that contains my .cpp file. I added the text file to project.
解决方案 Try closing the stream at the end of the function, it looks like the data isn't getting flushed.
outFile.close();
这篇关于outFile C ++不写入输出文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!