gpt4 book ai didi

c++ - 我可以对文件进行无格式写入,然后对整数类型的文件进行格式化读取吗?

转载 作者:行者123 更新时间:2023-11-28 05:37:37 25 4
gpt4 key购买 nike

鉴于我将整数的 vector 写入未格式化的临时文件:

ofstream foo("foo.txt", ios::binary);
const auto length= size(output);

foo.write(reinterpret_cast<const char*>(length), sizeof(length));

if(!empty(input)) foo.write(reinterpret_cast<const char*>(data(output)), sizeof(decltype(output)::value_type) * length);

我可以稍后从 inputoutput 类型相同的文件中执行以下格式化读取吗:

ifstream foo("foo.txt", ios::binary);
size_t size;

foo.read(reinterpret_cast<char*>(size), sizeof(size));
input.resize(size);

if(!empty(input)) copy(istream_iterator<decltype(output)::value_type>(foo), istream_iterator<decltype(output)::value_type>(), begin(input));

最佳答案

Can I do an Unformatted Write to a File Then a Formatted Read From a File for Integral Types?

不,你不能那样做。

使用无格式写入写入数据的文件内容与格式化写入有很大不同。格式化的读取无法准确解释未格式化的数据。

关于c++ - 我可以对文件进行无格式写入,然后对整数类型的文件进行格式化读取吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37889732/

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