Exception thrown: read access violation

Pages: 12
Thanks man.... I'll be back when I prove yet again I have no idea what I'm doing (probably sooner than later)
Ok, told you I'd be back with more questions.
here's the new function:
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
void printFile(fstream &file, string filename, InventoryCost inv[SIZE]) {
	Edit it;
	size_t code_length, edit_length;
	file.open(filename, ios::in | ios::binary);
	if (!file) {
		cout << "ERROR opening file!\n";
	}
	file.clear();
	file.read(reinterpret_cast<char *>(&it.itID), sizeof(it.itID));
	file.read(reinterpret_cast<char *>(code_length), sizeof(code_length));
	it.code.resize(code_length);
	file.read((char *)it.code.c_str(), code_length);
	file.read(reinterpret_cast<char *>(edit_length), sizeof(edit_length));
	it.edit.resize(edit_length);
	file.read((char *)it.edit.c_str(), edit_length);
	while (!file.eof()) {
		cout << it.itID << " " << it.code << " " << it.edit << endl;
		for (int i = 0; i < SIZE; i++) {
			if (inv[i].getID() == it.itID) {
				if (it.code == "AI") {
					int add = (stoi(it.edit));
					inv[i].setQty(add); 
				}
				else if (it.code == "RI") {
					int sub = (stoi(it.edit));
					inv[i].setQty(sub);
				}
				else if (it.code == "CW")
					inv[i].setWCost(stof(it.edit));
				else if (it.code == "CR")
					inv[i].setRCost(stof(it.edit));
				else if (it.code == "CD")
					inv[i].setDesc(it.edit);
				else
					cout << "Invalid Transaction Code\n";
			}
		}
                file.read(reinterpret_cast<char *>(&it.itID), sizeof(it.itID));
	        file.read(reinterpret_cast<char *>(code_length), sizeof(code_length));
	        it.code.resize(code_length);
	        file.read((char *)it.code.c_str(), code_length);
	        file.read(reinterpret_cast<char *>(edit_length), sizeof(edit_length));
	        it.edit.resize(edit_length);
	        file.read((char *)it.edit.c_str(), edit_length);
	}
	file.close();
}

I'm getting errors on lines 10 and 13. uninitialized local variable 'code_length' used. and same for 'edit_length'. I thought they are getting initialized...
Last edited on
I tried initializing the size_t's to the the size constraints of code and edit. It built ok, then after doing option 2 and moving on to option 3 i get yet another dialog box exclaiming:
Exception thrown at 0x59EE5147 (vcruntime140d.dll) in Greaney_Brian_CIS1202_FinalProject.exe:
 0xC0000005: Access violation reading location 0x00000002.

If there is a handler for this exception, the program may be safely continued.
Last edited on
Without seeing how the information was read it is hard to tell what is wrong with your code. However you are missing some ampersands on lines 10 and 13. Compare these two lines to line 9.

I really don't know what your trying to do in that loop without seeing what and how your writing the information to your file.

I don't understand why you're playing with the inventoryCost when you're trying to read data from a file. Why is this function called printFile() since it looks like this is really just reading a file. You really should simplify this function. You really should have a function that just reads the data from a file, and another file that just writes to a file. Strive to make your functions to one thing really well instead of doing several things adequately.

A small complete program that just writes some data and then reads that data back would be helpful in trouble shooting your problems.


Jim
Topic archived. No new replies allowed.
Pages: 12