c++ - Access violation error when getting input from a binary file -


okay, i'm trying read input binary file. i've changed code bit, version, i'm getting access violation error... it's trying access isn't there. here's source code problem area:

void hashfile::filedump (ostream &log) {     hashnode *temp = new hashnode;      fstream bin_file;     bin_file.open ("storage_file.bin", ios::in | ios::binary);        for(int = 0; < table_size; i++)     {         bin_file.seekg( * 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; } 

i know should have kind of error checking. right error occurring in printdump function. whenever try output log access violation error. however, change log cout , code run fine somewhat. read binary file i've created correctly until gets last element. i've been testing with, table_size should equal 5. loop , incremented until reaches 5 , keeps going. table_size being changed random value though haven't physically touched it. somehow writing on table_size's address in memory?

here definition of node:

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; }; 

this

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

should this

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

you getting confused on pointers.

whether code work depends on definition of node haven't shown.

also code leaks memory temp never deleted. better not allocate temp @ all, this

void hashfile::filedump (ostream &log) {     hashnode temp;      fstream bin_file("storage_file.bin", ios::in | ios::binary);        for(int = 0; < table_size; i++)     {         bin_file.seekg( * sizeof(hashnode) );          bin_file.read( (char *)&temp, sizeof(hashnode) );          printdump(hashnode(temp.title, temp.artist, temp.type, temp.year, temp.price), log, i);     } } 

not clear why feel need create new node temp, why not pass temp printdump? this

        printdump(temp, log, i); 

but without seeing definition of node can't sure.

also no need close file, happens automatically, opening file in constructor little cleaner imho.

edit

ok having seen definition of node recommendation

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

also change printdump use const reference, avoids copying node object (it quite big).

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; } 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -