2013-05-04 47 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