【问题标题】:Reading and writing to the same file fstream读取和写入同一个文件 fstream
【发布时间】:2016-09-11 13:01:50
【问题描述】:

我想更新现有的 json 文件。

这是示例 json 文件:

{
   "Foo": 51.32,
   "Number": 100,
   "Test": "Test1"
}

来自程序的日志:

Operation successfully performed
100
"Test1"
51.32
46.32
Done

看起来everythink按预期工作......

如果我将 fstream 更改为 ifstream 以读取,然后将 ofstream 更改为写入它正在工作......

我尝试使用调试器,但我发现 basic_ostream 对象中的数据有误...但我不知道为什么,我使用的字符串中的数据已更正(更新数据)。 知道有什么问题吗:-)?

【问题讨论】:

  • 调试器是解决此类问题的正确工具。 询问 Stack Overflow 之前,您应该逐行浏览您的代码。如需更多帮助,请阅读How to debug small programs (by Eric Lippert)。至少,您应该 [编辑] 您的问题以包含一个重现您的问题的 Minimal, Complete, and Verifiable 示例,以及您在调试器中所做的观察。
  • 在尝试重写其内容之前,您需要使用clear(),并且很可能需要使用seek()。请注意,如果新内容比现有内容短,这不会截断文件的大小,并且最后会出现尾随垃圾。
  • 发布更新:-)

标签: c++ stream ifstream ofstream


【解决方案1】:

这里有一些问题。

首先命令json json_data(fs); 读取到设置EOF 标志的文件末尾。在清除该标志之前,流将停止工作。

第二个文件指针在文件末尾。如果你想覆盖你需要重新回到开头的文件:

if (fs.is_open())
{
    json json_data(fs); // reads to end of file
    fs.clear(); // clear flag
    fs.seekg(0); // move to beginning

不幸的是,这仍然不能解决所有问题,因为如果您写回的文件比您读入的文件,那么一些旧数据将被标记到新数据的末尾:

    std::cout << "Operation successfully performed\n";
    std::cout << json_data.at("Number") << std::endl;
    std::cout << json_data.at("Test") << std::endl;
    std::cout << json_data.at("Foo") << std::endl;

    json_data.at("Foo") = 4.32; // what if new data is smaller?

Json 文件:

{
   "Foo": 4.32, // this number is smaller than before
   "Number": 100,
   "Test": "Test1"
}} // whoops trailing character from previous data!!

在这种情况下,我会简单地打开一个文件进行读取,然后打开另一个文件进行写入,这样更不容易出错并表达了覆盖所有内容的意图。

类似:

#include "json.hpp"
#include <iostream>
#include <fstream>
#include <string>

using json = nlohmann::json;

void readAndWriteDataToFile(std::string fileName) {

    json json_data;

    // restrict scope of file object (auto-closing raii)
    if(auto fs = std::ifstream(fileName))
    {
        json_data = json::parse(fs);

        std::cout << "Operation successfully performed\n";
        std::cout << json_data.at("Number") << std::endl;
        std::cout << json_data.at("Test") << std::endl;
        std::cout << json_data.at("Foo") << std::endl;
    }
    else
    {
        throw std::runtime_error(std::strerror(errno));
    }

    json_data.at("Foo") = 4.32;
    std::cout << json_data.at("Foo") << std::endl;
    std::string json_content = json_data.dump(3);

    if(auto fs = std::ofstream(fileName))
    {
        fs.write(json_content.data(), json_content.size());
        std::cout << "Done" << std::endl;
    }
    else
    {
        throw std::runtime_error(std::strerror(errno));
    }

}

int main()
{
    try
    {
        std::string fileName = "C:/new/json1.json";
        readAndWriteDataToFile(fileName);
    }
    catch(std::exception const& e)
    {
        std::cerr << e.what() << '\n';
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}

【讨论】:

  • 谢谢!所以我可以使用我的程序的第二个版本。现在我明白为什么这不起作用了。谢谢!
猜你喜欢
  • 2013-07-06
  • 2011-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-29
  • 2015-12-02
  • 1970-01-01
相关资源
最近更新 更多