access violation file handling URGENT HELP NEEDED PLZ :(

closed account (ybkG1hU5)
Write your question here.
kindly review the code ive written. It's giving breaks. plz reply guys! it's EXTREMELY urgent :'(


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
  #include <iostream>
#include <string>
#include <fstream>
#define N 5 //works for 3 lettered, 4 lettered, 5 lettered, 6 lettered & 7 lettered words
using namespace std;

struct node 
{
	string imageid, ans;
	node *next;
};

static node *ChainedHashTable[N];

class Questions
{
private: 
	node *curr;
public:
	Questions();
	~Questions();
	void hashtable(node *);
	int getkey(node *);
	void read(int);
	void write();
	void displayall();
	void display(node*);

};

Questions::~Questions()
{
	for (int i=0; i<N; i++)
     {
       node *curr = ChainedHashTable[i];
       while (curr != NULL)
       {
         node *discard = curr;
		 curr=curr->next;
		 delete discard;
	   }
	}
	//delete []ChainedHashTable;
}
Questions::Questions()
{
	curr = NULL;

for(int i=0; i<N; i++)
{
	ChainedHashTable[i]= NULL;
}
	    write();
  /*  node *temp=new node;
	ofstream Write("D:\\quiz.txt", ios::binary|ios::app);
	if ( !Write.is_open() )
	{
		cout<<":(";
	}
	temp->ans="cross";
	temp->imageid="1.jpg";
	Write.write(reinterpret_cast<char*>(temp), sizeof(node));*/

		node temp1;
		ifstream Read("D:\\quiz.txt", ios::binary| ios::app);
		Read.seekg(0,ios::beg); 
		Read.read(reinterpret_cast<char*>(&temp1),sizeof(node));
		while(!Read.eof())
		{
			cout<<temp1.ans;
			hashtable(&temp1);
			Read.read(reinterpret_cast<char*>(&temp1),sizeof(node));
		}
		Read.close();
		cout<<"YOUR PROGRAM IS RUNNING FINE ALHAMDULILLAH :)";
		//displayall();
}

int Questions:: getkey(node *a)
{return a->ans.length(); - 3;}


	void Questions:: hashtable(node *a)
	{ 
		int key = getkey(a);

		if (ChainedHashTable[key] == NULL)
		{
			ChainedHashTable[key] = new node;
			ChainedHashTable[key]->ans=a->ans;
			ChainedHashTable[key]->imageid=a->imageid;
			ChainedHashTable[key]->next=NULL;
		}

		else 
	   {
		 node *temp1= ChainedHashTable[key];
         while(temp1->next)
	   {                           
		   // scrolls down the list
			temp1 = temp1->next;
       }
		    node *temp= new node;// inserst the value as the last element of the list
			temp1->next=temp;
			temp->ans=a->ans;
			temp->imageid=a->imageid;
			temp->next = NULL;
		}
	}


	void Questions:: displayall()
	{
		for (int i = 0; i < N; i++)
		{
			cout<< i + 3 << "Letters! "<<endl;
			display(ChainedHashTable[i]);
		}
	}

	void Questions:: write() 
	{
	node *temp=new node;
	ofstream Write("D:\\quiz.txt", ios:: binary|ios::app);
	if ( !Write.is_open() )
	{
		cout<<":(";
	}
	temp->ans="crosscutter";
	temp->imageid="1.jpg";
	Write.write(reinterpret_cast<char*>(&temp), sizeof(node));
	temp->ans="signatures";
	temp->imageid="2.jpg";
	Write.write(reinterpret_cast<char*>(temp), sizeof(node));
	temp->ans="imaan";
	temp->imageid="3.jpg";
	Write.write(reinterpret_cast<char*>(temp), sizeof(node));
	temp->ans="mona";
	temp->imageid="4.jpg";
	Write.write(reinterpret_cast<char*>(temp), sizeof(node));
	temp->ans="ma";
	temp->imageid="5.jpg";
	Write.write(reinterpret_cast<char*>(temp), sizeof(node));
	Write.close();
	}

void Questions:: display(node *p)
	{
		while (p != NULL)
		{
			cout << p->ans << endl;
			p = p->next;
		}
	
	}
			

int main()
{
	Questions test;
	system ("PAUSE");
	return 0;
}
temp is a pointer. You are saving the same value of pointer 5 times in a row. And you should never save pointer values, as the are different each program execution.

And this is not to mention you are writing pointer, but expect to read node itself.
closed account (ybkG1hU5)
Thank you SO much :D :D :D
closed account (ybkG1hU5)
but it's still giving breakssss! :'( uwaaaaaaain
So, what your code looks like now?
closed account (ybkG1hU5)

void Questions:: write()
{
node temp;
ofstream Write("D:\\quiz.txt", ios:: binary|ios::app);
if ( !Write.is_open() )
{
cout<<":(";
}
temp.ans="crosscutter";
temp.imageid="1.jpg";
Write.write(reinterpret_cast<char*>(&temp), sizeof(node));
temp.ans="signatures";
temp.imageid="2.jpg";
Write.write(reinterpret_cast<char*>(&temp), sizeof(node));
temp.ans="imaan";
temp.imageid="3.jpg";
Write.write(reinterpret_cast<char*>(&temp), sizeof(node));
temp.ans="mona";
temp.imageid="4.jpg";
Write.write(reinterpret_cast<char*>(&temp), sizeof(node));
temp.ans="ma";
temp.imageid="5.jpg";
Write.write(reinterpret_cast<char*>(&temp), sizeof(node));
Write.close();
}
string imageid, ans; Objects of complex (not TriviallyCopyable) types cannot be saved by simple call to .write(). You need to serialize them separately.

I suggest to drop binary here an serialize as text.
closed account (ybkG1hU5)
you mean i should save them in a txt file?
NO, I mean that you do not need to use write() and binary mode.
File extension is irrelevant.

Something like that:

1
2
3
4
5
6
7
8
9
10
11
12
void node::save(std::ostream& out)
{
    out << imageid << '\n' << ans << '\n';
}

node node::load(std::istream& in)
{
    node temp;
    std::getline(in, temp.imageid);
    std::getline(in, temp.ans    );
    return temp;
}
closed account (ybkG1hU5)
got it got it. Thanks a lot @MiiNiPaa :)
Topic archived. No new replies allowed.