2013-05-04 45 views
0

好吧,所以我正在尝试从二进制文件读取输入。我已经改变了一下这个代码,但是在这个版本中,我得到了访问违规错误......所以它试图访问那些不存在的东西。这里是我的问题区域的源代码:从二进制文件获取输入时出现访问冲突错误

void HashFile::fileDump (ostream &log) 
{ 
    HashNode *temp = new HashNode; 

    fstream bin_file; 
    bin_file.open ("storage_file.bin", ios::in | ios::binary); 

    for(int i = 0; i < table_size; i++) 
    { 
     bin_file.seekg(i * sizeof(HashNode)); 

     bin_file.read((char *)&temp, sizeof(HashNode)); 

     printDump(HashNode(temp->title, temp->artist, temp->type, temp->year, 
     temp->price), log, i); 
    } 

    bin_file.close(); 
} 

void HashFile::printDump(HashNode A, ostream &log, int N) 
{ 
    log << "(" << N << ") " << A.title << ", " << A.artist 
     << ", " << A.type << ", " << A.year << ", $" 
     << setprecision(2) << A.price << endl; 
} 

我知道我应该进行某种错误检查。现在错误发生在printDump函数中。每当我尝试输出到日志时,我都会遇到访问冲突错误。但是,我将日志更改为cout,并且我的代码会稍微运行。它会读取我创建的二进制文件,直到它到达最后一个元素。对于我一直在测试的东西,table_size应该等于5.所以我进入for循环,并且我递增,直到它达到5,然后继续。即使我没有实际触及它,table_size也会被更改为一些随机值。我以某种方式在内存中写入table_size的地址?

这里是我的节点的定义:

class HashNode 
{ 
    public: 
     HashNode(); 
     ~HashNode(); 
     HashNode(string title, string artist, string type, int year, float price); 
     friend class HashFile; 
    private: 
     char title [35]; 
     char artist [25]; 
     char type [12]; 
     int year; 
     float price; 
}; 

回答

0

bin_file.read((char *)&temp, sizeof(HashNode)); 

应该是这个

bin_file.read((char *)temp, sizeof(HashNode)); 

你感到困惑了指针。

该代码是否会实际工作取决于您没有显示的Node的定义。

此外代码泄漏内存,因为临时不会被删除。这将是最好不要在所有分配温度,这样

void HashFile::fileDump (ostream &log) 
{ 
    HashNode temp; 

    fstream bin_file("storage_file.bin", ios::in | ios::binary); 

    for(int i = 0; i < table_size; i++) 
    { 
     bin_file.seekg(i * sizeof(HashNode)); 

     bin_file.read((char *)&temp, sizeof(HashNode)); 

     printDump(HashNode(temp.title, temp.artist, temp.type, temp.year, temp.price), log, i); 
    } 
} 

尚不清楚为什么你觉得有必要从temp创建一个新的节点,为什么不只是通过临时到printDump?像这样

 printDump(temp, log, i); 

但是没有看到节点的定义我不能肯定地说。

也不需要关闭文件,这自动发生,也打开构造函数中的文件是一个更清洁恕我直言。

编辑

看过OK的Node的定义,这将是我的建议

void HashFile::fileDump(ostream &log) 
{ 
    fstream bin_file("storage_file.bin", ios::in | ios::binary); 
    for(int i = 0; i < table_size; i++) 
    { 
     bin_file.seekg(i * sizeof(HashNode));  
     HashNode temp; 
     bin_file.read((char *)&temp, sizeof(HashNode)); 
     printDump(temp, log, i); 
    } 
} 

而且我会改变printDump使用const引用,这样就避免了复制Node对象(它是挺大)。

void HashFile::printDump(const HashNode& A, ostream &log, int N) 
{ 
    log << "(" << N << ") " << A.title << ", " << A.artist 
     << ", " << A.type << ", " << A.year << ", $" 
     << setprecision(2) << A.price << endl; 
} 
+0

我添加了Node的定义。在这一点上,我质疑为什么我以某种方式做事。我仍然在学习如何编程,所以请原谅我的新手错误。 – user2349812 2013-05-04 13:32:42

+0

@ user2349812没问题,指针很混乱。原始代码中的错误是,当你用new分配'temp'时,你已经有了一个指针,所以你不需要用'&'来创建另一个指针。但是在我最近的代码中,'temp'不是一个指针,所以你需要'&'来创建一个指针来传递。 – john 2013-05-04 13:34:46

+0

好吧,我刚刚完成了一切编辑(使它不会将它们作为指针存储在二进制文件中)并且它完全运行!非常感谢您的帮助和解释。 – user2349812 2013-05-04 13:44:58