【发布时间】:2014-12-12 00:09:58
【问题描述】:
我正在尝试编写一对写/读函数,分别用于在二进制文件中存储/检索std::set< T > 数据容器。我的writeSet() 函数似乎可以正常工作,并且可以毫无问题地存储std::set< T > 数据容器。但是readSet() 函数没有,并抛出“未处理的异常 [...] 访问冲突读取位置 0xFEEEEF6”。 main() 函数返回 EXIT_SUCCESS 之后的异常。
据我调试(使用 Visual Studio 2013),readSet() 实际上似乎成功读取二进制文件并将数据正确分配到寻址的std::set< T > 数据容器。当 VS2013 崩溃时,它为我提供了中断程序执行的机会,并指出 xtree.h 头文件中的错误。具体来说,调试器抱怨_Pnext 以下部分的|| _Ptr != 0 && (*_Pnext)->_Ptr != _Ptr) 行:
#if _ITERATOR_DEBUG_LEVEL == 2
void _Orphan_ptr(_Myt& _Cont, _Nodeptr _Ptr) const
{ // orphan iterators with specified node pointers
_Lockit _Lock(_LOCK_DEBUG);
const_iterator **_Pnext = (const_iterator **)_Cont._Getpfirst();
if (_Pnext != 0)
while (*_Pnext != 0)
if ((*_Pnext)->_Ptr == this->_Myhead
|| _Ptr != 0 && (*_Pnext)->_Ptr != _Ptr)
_Pnext = (const_iterator **)(*_Pnext)->_Getpnext();
else
{ // orphan the iterator
(*_Pnext)->_Clrcont();
*_Pnext = *(const_iterator **)(*_Pnext)->_Getpnext();
}
我的最小工作解决方案如下:
writeSet() : 将 std::set 写入二进制文件
template < class T >
void writeSet(const std::string& filePath, const std::string fileName, std::set<T>& data)
{
// Construct binary file location string using filePath and fileName inputs
std::stringstream file; file << filePath.c_str() << "/" << fileName.c_str();
std::ofstream fileStream; fileStream.open(file.str().c_str(), std::ios::out | std::ios::binary);
// First write number of std::set elements held by data, and then write std::set block to binary file
std::set<T>::size_type n = data.size();
fileStream.write(reinterpret_cast<char*>(&n), sizeof(std::set<T>::size_type));
fileStream.write(reinterpret_cast<char*>(&data), sizeof(T)*n);
fileStream.close();
}
readSet():从二进制文件中读取 std::set
template < class T >
void readSet(const std::string& filePath, const std::string fileName, std::set<T>& data)
{
// Construct binary file location string using filePath and fileName inputs
std::stringstream file; file << filePath.c_str() << "/" << fileName.c_str();
std::ifstream fileStream; fileStream.open(file.str().c_str(), std::ios::in | std::ios::binary);
// First read number of elements stored in binary file, then write file content to adresses std::set data variable
std::set<T>::size_type n;
fileStream.read(reinterpret_cast<char*>(&n), sizeof(std::set<T>::size_type));
fileStream.read(reinterpret_cast<char*>(&data), sizeof(T)*n);
fileStream.close();
}
main() :重现“未处理异常”错误的最小主函数
#include <fstream>
#include <iostream>
#include <set>
#include <sstream>
#include <string>
int main()
{
// Define binary file read/write directory
const std::string path("C:/data");
// writeSet() testing...
std::set<int> writeSetData;
writeSetData.insert(1); writeSetData.insert(3);
writeSetData.insert(0); writeSetData.insert(2);
writeSet(path, "binaryFile", writeSetData);
// readSet() testing...
std::set<int> readSetData;
readSet(path, "binaryFile", readSetData);
std::cout << "readSetData= {";
for (std::set<int>::iterator it = readSetData.begin(); it != readSetData.end(); ++it){
std::cout << *it << ", ";
} std::cout << "}" << std::endl;
return EXIT_SUCCESS;
}
【问题讨论】:
标签: c++ set std binaryfiles