Unhandled exception, file stream

It displays alright. But then when I try to terminate the program it throws exception "Unhandled exception at 0x5036CCC8 (msvcp110d.dll) in VotingSys.exe: 0xC0000005: Access violation reading location 0x0045A2AC."

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
 
class Candidate:private Person //'string name' and 'int age' are derived from class Person
{
private:
	int vote_recieved;
	string party;
public:
	void operator ++(int);
	void display(void);
	void getdata(void);
};


void Candidate::operator ++(int)
{
	vote_recieved++;
}

void Candidate::display()
{
	cout << name << setw(10) << "|" << setw(5) << age << setw(5) << "|" << setw(5) << party << endl;
}

void Candidate::getdata(void)
{
	cout << "Enter Name";
	getline(cin,name);
	cout << "Party";
	getline(cin,party);
	cout << "Age";
	cin >> age;
}

int main()
{
	ifstream file;
	Candidate D;
	file.open("Test.txt",ios::binary);
	file.seekg(0);
	while (!file.eof()){
	file.read(reinterpret_cast<char *>(&D),sizeof(D));
	}
	file.close();
	D.display();
	char r = getch();
	return 0;
}
Last edited on
Line 41: You can't do a raw (binary) read of a std::string. A std::string contains an internal pointer to dynamically allocated memory. The value of the pointer when you wrote it out is going to be meaningless when you attampt to read back the base struct of a std::string. You're not going to get the memory the string object pointed to.

Since you didn't post the declaration of Person, it's impossible to tell if you also have the same problem in the base class. I'm guessing that you do, since you have references to name and age, which don't appear in Candidate.

You can accomplish what you want by:
 
  while (file >> vote_received >> party)

This does not however read any base class members (if any). Again, Person is not shown.

BTW, what is the point of the loop at line 40? If you read more than one Candidate from the file, display() is only going to display the last one.




Last edited on
Sorry for not posting base class. Actually the class Person is an abstract class containing 'string name' and 'int age'.

And, the loop was actually intended for array of Objects of class Candidate. This piece of code was just for test. :)

As you mentioned about the string. Do you have any suggestion for the problem?

Anyways thanks for the reply.
Do you have any suggestion for the problem?

I made a suggestion above (at least for Candidate).

Assuming name and age are protected members of Person, you could do the following:
 
  while (file >>name >> age >> vote_received >> party)

This assumes the above variables are written out in text mode, not binary mode.

However, this isn't the most desirable solution though, since Candidate now references members of Person. i.e. If you add another variable to Person, you must change Candidate.

A better solution is to provide a read function for each object:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Person
{  string name;
    int age; 
public:
...
  virtual istream & Read (istream & is)
  { return (is >> name >> age);
  }
};

class Candidate
{  int vote_received;
    string party;
public:
...
  istream & Read (istream & is)
  {  // Invoke the base class' Read function
      if (!  Person::Read (is))   // Read the Person portion
        return is;  // Couldn't read Person portion 
     return (is >> vote_received >> party);
  }
};

Topic archived. No new replies allowed.