Reputation: 21
I get this error " 0xC0000005: Access violation writing location 0xfeeefeee" in c++ program.
My code is
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class Employee
{
public:
string name;
int age;
int phone;
int salary;
};
int main()
{
Employee emp1;
ofstream f1;
f1.open("qwe.txt",ios::binary|ios::app);
for(int i=0;i<2;i++)
{
cout<<"enter name:\t";
cin>>emp1.name;
cout<<"enter age:\t";
cin>>emp1.age;
cout<<"enter phone:\t";
cin>>emp1.phone;
cout<<"enter salary:\t";
cin>>emp1.salary;
cout<<"\n";
f1.write((char *)(&emp1),sizeof(Employee));
}
f1.close();
Employee emp2;
ifstream f2;
f2.open("qwe.txt",ios::binary|ios::in);
while(f2)
{
f2.read((char *)(&emp2),sizeof(Employee));
if(f2.eof())
{
break;
}
else
{
cout<<"\n"<<emp2.name;
cout<<"\n"<<emp2.age;
cout<<"\n"<<emp2.phone;
cout<<"\n"<<emp2.salary<<"\n";
}
}
f2.close();
cin.get();
return 0;
}
I think the problem is in while(f2)
. But I am not sure. This line f2.read((char *)(&emp2),sizeof(Employee))
may create problem. But I need this line.
Upvotes: 1
Views: 818
Reputation: 815
If you need a low level API as used in your code(read/write) check out:
Be careful, the following line can break your data, I was changed this to avoid the previous valued writed by you.
f1.open("qwe.txt", ios::binary | ios::trunc);
Complete code:
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;
class Employee {
public:
string name;
int age;
int phone;
int salary;
};
int main() {
Employee emp1;
ofstream f1;
f1.open("qwe.txt", ios::binary | ios::trunc);
for (int i = 0; i < 2; i++)
{
cout << "enter name:\t";
cin >> emp1.name;
cout << "enter age:\t";
cin >> emp1.age;
cout << "enter phone:\t";
cin >> emp1.phone;
cout << "enter salary:\t";
cin >> emp1.salary;
cout << "\n";
size_t lenght = emp1.name.size();
char lenghtval[sizeof(lenght)];
std::memcpy(&lenghtval, &lenght, sizeof(lenght));
f1.write(lenghtval, sizeof(lenght));
const char *name = emp1.name.c_str();
f1.write(name, static_cast<int>(lenght));
int val = emp1.age;
char towrite[sizeof(val)];
std::memcpy(&towrite, &val, sizeof(val));
f1.write(towrite, sizeof(val));
val = emp1.phone;
std::memcpy(&towrite, &val, sizeof(val));
f1.write(towrite, sizeof(val));
val = emp1.salary;
std::memcpy(&towrite, &val, sizeof(val));
f1.write(towrite, sizeof(val));
}
f1.close();
Employee emp2;
ifstream f2;
f2.open("qwe.txt", ios::binary | ios::in);
while (f2) {
size_t lenght = 0;
char lenghtval[sizeof(lenght)];
f2.read(lenghtval, sizeof(lenght));
std::memcpy(&lenght, lenghtval, sizeof(lenght));
char name[lenght + 1];
f2.read(name, static_cast<int>(lenght));
name[lenght] = '\0';
emp2.name = name;
int val = 0;
char toread[sizeof(val)];
f2.read(toread, sizeof(val));
std::memcpy(&val, toread, sizeof(val));
emp2.age = val;
f2.read(toread, sizeof(val));
std::memcpy(&val, toread, sizeof(val));
emp2.phone = val;
f2.read(toread, sizeof(val));
std::memcpy(&val, toread, sizeof(val));
emp2.salary = val;
if (f2.eof()) {
break;
}
cout << "\n" << emp2.name << std::endl;
cout << "\n" << emp2.age;
cout << "\n" << emp2.phone;
cout << "\n" << emp2.salary << "\n";
}
f2.close();
cin.get();
return 0;
}
Upvotes: 0
Reputation: 5920
You could not read/write structures with a complex types, like std::string
that way. They have an internal structure which is implementation specific. Directly overriding its memory is a surest way to shoot yourself in the foot. Use >>
/<<
operators instead:
f1 << emp1.name;
f1 << emp1.age;
//...
f2 >> emp2.name;
f2 >> emp2.age;
Upvotes: 2
Reputation: 13690
The internal representation of a class is implementation defined. Additionally the string
member can hold the data in the member directly or in an additional heap object depending of the number of charaters.
That's why you need a serialization of instances of your class. The serialize function will take serialization target like ofstream
an writes a represenation of the data. The deserialization function will take a serialization source like ifstream
and reads the reprensentation to the members.
Upvotes: 1