CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2011
    Posts
    1

    Exclamation Binary file read>> debug assertion failed..

    I'm trying to compile this code, and I get a pop up on my screen saying something like this:
    *************************************
    DEBUG ASSERTION FAILED!

    Pogram: "my program"

    File: f:\... \dbgdel.cpp
    Line: 52

    Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

    *************************************

    I know that reading from the binary file is causing the problem LINE 148(I removed the line from the code and everythig works fine) .. in fact, with out removing that line all the operations are succesfully executed... when using the debugger, the program stops on the return0; line from main()... I HOPE SOMEONE COULD HELP!!

    *************************************

    #include <iostream>
    #include <string>
    #include <fstream>
    #include <cstdlib>

    using namespace std;

    #define MAXBUCKETS 30
    #define MAXSLOTS 3
    #define OVERFLOW 10


    template <class KEY, class DATA>
    class SLOT
    {
    protected:
    KEY key;
    DATA data;
    public:

    KEY GetKey ()
    { return key; }

    DATA GetData ()
    {return data; }

    void SetKey(KEY k)
    { key = k; }

    void SetData(DATA d)
    { data = d; }
    };


    template <class SLOTS, class KEY, class DATA>
    class BUCKET : private SLOT <string, string>
    {
    protected:
    SLOTS ht_slot[MAXSLOTS];
    unsigned int count;
    unsigned short int overflow;

    public:


    BUCKET ()
    { count = overflow = 0;}

    unsigned int GetCount()
    { return count; }

    unsigned short int GetOverflow()
    { return overflow; }

    void SetSlot(int i, KEY k, DATA d)
    {
    ht_slot[i].SetKey(k);
    ht_slot[i].SetData(d);
    cout << ht_slot[i].GetKey()<< endl;
    }

    KEY GetSlotKey (int i)
    { return ht_slot[i].GetKey(); }




    void SetCount (unsigned int c)
    { count = c; }

    void AddCount ()
    { count = ++count; }

    void SetOverflow (unsigned int o)
    { overflow = o; }


    };


    class HT : private BUCKET<SLOT<string, string>, string, string>
    {
    protected:
    BUCKET hash_table[MAXBUCKETS];
    int index, collisions;
    string tempdata;
    bool stored;

    void WriteToSlot(int index);

    public:

    HT()
    {
    index = collisions = 0;
    tempdata.clear();
    stored = false;
    }

    void HT::InsertData (fstream &DATAFILE);



    int Hash (string s);

    void SetSlot (int b_i, int s_i, string k, string d)
    {
    hash_table[b_i].SetSlot(s_i,k,d);
    }

    string GetKey (int b_i, int s_i)
    { return hash_table[b_i].GetSlotKey(s_i); }

    unsigned int GetCount(int b_i)
    { return hash_table[b_i].GetCount(); }
    };

    void CheckFile (fstream &DATAFILE);

    void PrintData(HT &ht);

    int main ()
    {
    fstream DATAFILE, SAVEFILE;
    HT ht;

    DATAFILE.open("DATAIN.txt", ios::in);
    CheckFile(DATAFILE);

    ht.InsertData(DATAFILE);
    PrintData(ht);

    SAVEFILE.open("SAVEFILE.dat", ios:ut | ios::binary);
    CheckFile(SAVEFILE);

    SAVEFILE.write(reinterpret_cast<char*>(&ht), sizeof(ht));

    SAVEFILE.close();


    SAVEFILE.open("SAVEFILE.dat", ios::in | ios::binary);
    HT ht2;


    SAVEFILE.seekg(0L, ios::end);
    long int buffer = SAVEFILE.tellg();
    SAVEFILE.seekg(0L, ios::beg);
    SAVEFILE.read(reinterpret_cast<char*>(&ht2), buffer);

    SAVEFILE.close();

    PrintData(ht2);


    return 0;
    }

    void CheckFile (fstream &FILE)
    {
    if (!FILE.is_open())
    {
    cout << "Unable to open FILE, the program will now shut down...";
    exit(1);
    }
    }

    void PrintData(HT &ht)
    {
    unsigned int i, j;
    for (i = 0; i < MAXBUCKETS; i++)
    {
    cout << "\n\nBUCKET #" << i+1 << endl;
    for (j = 0; j < ht.GetCount(i) ; j++)
    { cout << "SLOT #" << j+1 << " " << ht.GetKey(i,j)<< endl; }
    for (j; j < MAXSLOTS; j++)
    { cout << "SLOT #" << j+1 << " " << "'EMPTY'"<< endl; }
    }
    }

    int HT::Hash (string s)
    {
    int index, char_sum;

    char_sum = s[1] + s[3] + s[5];
    index = char_sum % (MAXBUCKETS-OVERFLOW);

    return index;
    }



    void HT::InsertData (fstream &DATAFILE) //(fstream &DATAFILE, BUCKET *hash_table, int &collisions)
    {

    while (!DATAFILE.eof())
    {
    stored = false;
    tempdata.clear();

    getline(DATAFILE, tempdata);
    cout << tempdata << "\n\n";
    index = Hash(tempdata.substr(0,10));

    while (!stored)
    {
    if (hash_table[index].GetCount() < MAXSLOTS)
    {
    SetSlot(index, hash_table[index].GetCount(), tempdata.substr(0,10), tempdata.substr(10));
    hash_table[index].AddCount();
    cout << endl << index << "\t";
    cout << hash_table[index].GetCount() << endl << endl;

    stored = true;
    }
    else if (hash_table[index].GetOverflow() != 0)
    {
    cout << "\n\noverflow" << index;
    index = hash_table[index].GetOverflow();
    }
    else
    {
    cout << "\n\nnooverflow" << index << "\n\n";
    for (int i = (MAXBUCKETS - OVERFLOW); i < MAXBUCKETS; i++)
    {
    if (hash_table[i].GetCount() < MAXSLOTS)
    {
    SetSlot(i, hash_table[i].GetCount(), tempdata.substr(0,10), tempdata.substr(10));

    cout << hash_table[i].GetSlotKey(hash_table[i].GetCount());

    hash_table[i].AddCount();


    cout << endl << index << "\t";
    cout << hash_table[index].GetCount() << endl << endl;
    cout << endl << i << "\t";
    cout << hash_table[i].GetCount() << endl << endl;

    hash_table[index].SetOverflow(i);
    stored = true;
    if (i = MAXBUCKETS-1)
    i=0;
    break;
    }
    }
    }
    }
    }

    }

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: Binary file read>> debug assertion failed..

    Code:
    SAVEFILE.read(reinterpret_cast<char*>(&ht2), buffer);
    ht2 is an object of type HT. HT is non POD (plain old data).
    You can not read/write this way for non POD. (constructors
    are not called, etc. )

    sizeof(string) is a constant, regardless of how many characters
    are actually in the string, so a binary read/write will not work.

    Your options:

    1) use text I/O
    2) read/write indivdual members of the object in binary. For objects
    such as std::string you need to write special functions.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Binary file read>> debug assertion failed..

    Quote Originally Posted by pristrer View Post
    I'm trying to compile this code,
    Please, for the sake of everyone reading your post, use code tags when posting code!

    The code you posted is unformatted and practically unreadable. I'll do you a favor -- here is your code in code tags
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <cstdlib>
    
    using namespace std;
    
    #define MAXBUCKETS 30
    #define MAXSLOTS 3
    #define OVERFLOW 10
    
    
    template <class KEY, class DATA> 
    class SLOT
    {
    protected:
    	KEY key;
    	DATA data;
    public:
    
    	KEY GetKey ()
    	{ return key; }
    
    	DATA GetData ()
    	{return data; }
    
    	void SetKey(KEY k)
    	{ key = k; }
    
    	void SetData(DATA d)
    	{ data = d; }
    };
    
    
    template <class SLOTS, class KEY, class DATA>
    class BUCKET : private SLOT <string, string>
    {
    protected:
    	SLOTS ht_slot[MAXSLOTS];
    	unsigned int count;
    	unsigned short int overflow;
    
    public:
    
    
    	BUCKET ()
    	{ count = overflow = 0;}
    
    	unsigned int GetCount()
    	{ return count; }
    
    	unsigned short int GetOverflow()
    	{ return overflow; }
    
    	void SetSlot(int i, KEY k, DATA d)
    	{
    		ht_slot[i].SetKey(k);
    		ht_slot[i].SetData(d);
    		cout << ht_slot[i].GetKey()<< endl;
    	}
    	
    	KEY GetSlotKey (int i)
    	{ return ht_slot[i].GetKey(); }
    
    
    
    
    	void SetCount (unsigned int c)
    	{ count = c; }
    
    	void AddCount ()
    	{ count = ++count; }
    
    	void SetOverflow (unsigned int o)
    	{ overflow = o; }
    
    
    };
    
    
    class HT : private BUCKET<SLOT<string, string>, string, string>
    {
    protected:
    	BUCKET hash_table[MAXBUCKETS];
    	int index, collisions;
    	string tempdata;
    	bool stored;
    
    	void WriteToSlot(int index);
    
    public:
    
    	HT()
    	{
    		index = collisions = 0;
    		tempdata.clear();
    		stored = false;
    	}
    
    	void HT::InsertData (fstream &DATAFILE);
    
    
    
    	int Hash (string s);
    
    	void SetSlot (int b_i, int s_i, string k, string d)
    	{
    		hash_table[b_i].SetSlot(s_i,k,d);
    	}
    
    	string GetKey (int b_i, int s_i)
    	{ return hash_table[b_i].GetSlotKey(s_i); }
    
    	unsigned int GetCount(int b_i)
    	{ return hash_table[b_i].GetCount(); }
    };
    
    void CheckFile (fstream &DATAFILE);
    
    void PrintData(HT &ht);
    
    int main ()
    {
    	fstream DATAFILE, SAVEFILE;
    	HT ht;
    	
    	DATAFILE.open("DATAIN.txt", ios::in);
    	CheckFile(DATAFILE);
    
    	ht.InsertData(DATAFILE);
    	PrintData(ht);
    
    	SAVEFILE.open("SAVEFILE.dat", ios::out | ios::binary);
    	CheckFile(SAVEFILE);
    
    	SAVEFILE.write(reinterpret_cast<char*>(&ht), sizeof(ht));
    
    	SAVEFILE.close();
    
    
    	SAVEFILE.open("SAVEFILE.dat", ios::in | ios::binary);
    	HT ht2;
    
    
    	SAVEFILE.seekg(0L, ios::end);
    	long int buffer = SAVEFILE.tellg();
    	SAVEFILE.seekg(0L, ios::beg);
    	SAVEFILE.read(reinterpret_cast<char*>(&ht2), buffer);
    
    	SAVEFILE.close();
    
    	PrintData(ht2);
    
    
    	return 0;
    }
    
    void CheckFile (fstream &FILE)
    {
    	if (!FILE.is_open())
    	{
    		cout << "Unable to open FILE, the program will now shut down...";
    		exit(1);
    	}
    }
    
    void PrintData(HT &ht)
    {
    	unsigned int i, j;
    	for (i = 0; i < MAXBUCKETS; i++)
    	{
    		cout << "\n\nBUCKET #" << i+1 << endl;
    		for (j = 0; j < ht.GetCount(i) ; j++)
    		{ cout << "SLOT #" <<  j+1 << "  " << ht.GetKey(i,j)<< endl; }
    		for (j; j < MAXSLOTS; j++)
    		{ cout << "SLOT #" <<  j+1 << "  " << "'EMPTY'"<< endl; }
    	}
    }
    
    int HT::Hash (string s)
    {
    	int index, char_sum;
    
    	char_sum = s[1] + s[3] + s[5];
    	index = char_sum % (MAXBUCKETS-OVERFLOW);
    
    	return index;
    }
    
    
    
    void HT::InsertData (fstream &DATAFILE)  //(fstream &DATAFILE, BUCKET *hash_table, int &collisions)
    {
    
    	while (!DATAFILE.eof())
    	{
    		stored = false;
    		tempdata.clear();
    
    		getline(DATAFILE, tempdata);
    		cout << tempdata << "\n\n";
    		index = Hash(tempdata.substr(0,10));
    	
    		while (!stored)
    		{
    			if (hash_table[index].GetCount() < MAXSLOTS)
    			{
    				SetSlot(index, hash_table[index].GetCount(), tempdata.substr(0,10), tempdata.substr(10));
    				hash_table[index].AddCount();
    				cout << endl << index << "\t";
    				cout << hash_table[index].GetCount() << endl << endl;
    				
    				stored = true;
    			}
    			else if (hash_table[index].GetOverflow() != 0)
    			{
    				cout << "\n\noverflow" << index;
    				index = hash_table[index].GetOverflow();
    			}
    				else
    				{
    					cout << "\n\nnooverflow" << index << "\n\n";
    					for (int i = (MAXBUCKETS - OVERFLOW); i < MAXBUCKETS; i++)
    					{
    						if (hash_table[i].GetCount() < MAXSLOTS)
    						{
    							SetSlot(i, hash_table[i].GetCount(), tempdata.substr(0,10), tempdata.substr(10));
    
    							cout << hash_table[i].GetSlotKey(hash_table[i].GetCount());
    
    							hash_table[i].AddCount();
    
    
    							cout << endl << index << "\t";
    							cout << hash_table[index].GetCount() << endl << endl;
    							cout << endl << i << "\t";
    							cout << hash_table[i].GetCount() << endl << endl;
    
    							hash_table[index].SetOverflow(i);
    							stored = true;
    							if (i = MAXBUCKETS-1)
    								i=0;
    							break;
    						}
    					}
    				}
    		}
    	}
    
    }
    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Binary file read>> debug assertion failed..

    Quote Originally Posted by Philip Nicoletti View Post
    Code:
    SAVEFILE.read(reinterpret_cast<char*>(&ht2), buffer);
    Seemingly, this erroneous code has made it big around the novice C++ community. I have seen many posts from new programmers making this exact same mistake. If you did a CG search, you'll come across this bad code over and over again.

    From what I hear, it is from a textbook, but don't remember which one.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Binary file read>> debug assertion failed..

    Quote Originally Posted by pristrer View Post
    I'm trying to compile this code, and I get a pop up on my screen saying something like this:
    *************************************
    DEBUG ASSERTION FAILED!

    Pogram: "my program"

    File: f:\... \dbgdel.cpp
    Line: 52

    Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

    *************************************

    I know that reading from the binary file is causing the problem LINE 148(I removed the line from the code and everythig works fine) .. in fact, with out removing that line all the operations are succesfully executed... when using the debugger, the program stops on the return0; line from main()... I HOPE SOMEONE COULD HELP!!
    As Philip pointed out, you can't write to binary files as you're doing now if the type is non-POD. You need to take each member of the class/struct and actually write the data that each member represents. For example, if it's a string, then you write the characters, you don't write the string object.

    Regards,

    Paul McKenzie

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured