免责声明:这只是初学者的另一项任务,所以我很高兴在不同的编译器和不同的架构上我可能会得到不同的(不兼容的)二进制文件。所以没关系,它只适用于这台特定的机器。
我正在写入和读取size_t
二进制文件。写作看起来像:
std::string result;
result.append((char *)&block_size_, sizeof(block_size_));
不久之后将result
其写入文件。
但是当我以同样的方式阅读它时:
map_binary.copy((char *)&block_size_, sizeof(block_size_), offset);
我收到警告
warning C4996: 'std::basic_string<_Elem,_Traits,_Alloc>::copy': Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Alloc=std::allocator<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring(1777) : see declaration of 'std::basic_string<_Elem,_Traits,_Alloc>::copy'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Alloc=std::allocator<char>
1> ]
我不明白为什么这段代码不安全并寻找解决方案,而不是假装根本没有问题(使用-D_SCL_SECURE_NO_WARNINGS
)。
那么,我错过了什么?
PS:现在我只使用标准库学习 C++,所以使用boost
或其他东西的解决方案是不可接受的。