C++ 输入文件没有将我的整个文件读回字符串

标签 c++ c++11

我有一个程序正在接收一个 JSON 文件,总共大约有 170,000 个字符。我将它们全部保存到一个文本文件中(这部分工作正常)然后我试图将整个文本文件读回一个字符串:

json::value historicalData = value;
    ofstream outputFile("test.txt"); //output to file
    outputFile << historicalData; //store JSON into file
    ifstream inputFile("test.txt"); //input from file
    string historicalDataString="";
    string appendTemp;
    while(!inputFile.eof()){
    getline(inputFile,appendTemp); //store value from file into string
    historicalDataString.append(appendTemp);
    }

当我使用上面的这段代码读回文件时,我没有得到文件的整个长度。它停止了大约三分之二的方式,并在我的字符串末尾附加了一个“1”。我做错了什么?

最佳答案

outputFile.close();

解决了我的问题。

关于C++ 输入文件没有将我的整个文件读回字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49888321/

相关文章:

c++ - C++11 中的正则表达式

c++ - 为什么 C++11 中没有 `static_if`

eclipse - Eclipse 和 c++11 中的线程

c++ - 类型转换和物体布局

c++ - openGL在不同的显示模式下绘制圆形三角形和正方形

双重嵌套模板类中的 c++ static int def 因 clang++ 和 g++ 而失败

c++ - "The Rule of Zero"是否也适用于具有虚方法的类?

c++ - 参数包函数参数可以默认吗?

c++ - C++ 中的符号数据类型

c++ - 格式化读取文件数据C++