gpt4 book ai didi

C++:使用 Boost 序列化来写入/读取文件

转载 作者:行者123 更新时间:2023-11-30 02:40:50 28 4
gpt4 key购买 nike

<分区>

我需要写入/读取包含 std::map 的文件。该文件必须在程序启动时读取(如果存在)。我正在使用 boost 的 fstream,但我得到了这个:

"terminate called after throwing an instance of 'boost::archive::archive_exception'
what(): input stream error"

好吧,我真的不知道发生了什么..这些是我的台词:

map<int64_t, int64_t> foo;
filesystem::path myFile = GetWorkingDirectory() / "myfile.dat";
[...............] // some code

filesystem::ifstream ifs(myFile);
archive::text_archive ta(ifs);
if (filesystem::exists(myFile)
{
ta >> foo; // foo is empty until now, it's fed by myFile
ifs.close();
}

我做错了什么?任何想法?谢谢。

附言请注意,之后的几行,我需要执行相反的操作:将 std::map foo 写入 myfile.dat。

编辑:如果我使用 std::ifstream,一切正常,将文件保存在我运行应用程序的同一目录中。但是使用 boost 和他的路径,出了点问题。

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