c++ - outFile C++ 不写入输出文本

标签 c++ format fstream file-io

下面是我不完整的程序。我在写入文本文件时遇到问题。例如,我想将下雪天数写入一个文本文件,但当我在 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/

相关文章:

c++ - 期待 eof 但 eof 附近的任意数据

c++ - 更新 .h 文件时,是否应该更新库?

zend-framework - Zend Framework,将 URL 的扩展名映射到格式参数?

Java随机生成两位小数格式的价格

C++ Fstream 只打印一个字

c++ - 如何修复使用 std::cin 函数后从文件读取的问题

c++ - 从 fstream 读取十六进制值到 int

c++ - C++中的C代码不兼容参数错误

c++ - 无需等待在命令行应用程序中按下输入即可获取键盘事件

git - 使用 hg Convert 将 git repo 转换为旧的 svn 格式