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!!

*************************************
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#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;
						}
					}
				}
		}
	}

}
You have an std::string in your class, which means your .read()/write() methods are incorrect. You'll actually have to write a save/load function yourself.
would you mind explaining how do I have to do that.. . This is the first time I'm attempting something like this.. THANK YOU!!
Do you know how to use fstreams? If you don't, go look it up. After that, see if you can figure out how to use them in this case.
Topic archived. No new replies allowed.