gpt4 book ai didi

c++ - 读写同一个文件fstream

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:15:18 24 4
gpt4 key购买 nike

我想更新现有的 json 文件。

这是示例 json 文件:

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

程序日志:

Operation successfully performed
100
"Test1"
51.32
46.32
Done

看起来一切都按预期工作......

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

我尝试使用调试器,正如我所见,basic_ostream 对象中的数据有误...但我不知道为什么,我使用来自字符串的经过更正(更新的数据)的数据。知道出了什么问题 :-) 吗?

最佳答案

这里有一些问题。

首先命令 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;
}

关于c++ - 读写同一个文件fstream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39436371/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com