I'm working on getting an understanding of Binary file I/O but have run into a stumbling block that I can't quite figure out.
Essentially what I have is a simple program that allows yoo to instantiate an object, add information to it, write that info to a file in either a sequential or binary format and then load it back into the program to be displayed.
The sequential portion works fine for saving, loading and displaying, it's just the binary portion where it's hurting my head.
What happens is this:
1st Run:
So far so good...
2nd Run:
Boom, for some reason the program segfaults whenever I try to view the data that was loaded. Any pointers would be greatly appreciated!
The code is below:
Essentially what I have is a simple program that allows yoo to instantiate an object, add information to it, write that info to a file in either a sequential or binary format and then load it back into the program to be displayed.
The sequential portion works fine for saving, loading and displaying, it's just the binary portion where it's hurting my head.
What happens is this:
1st Run:
Run program,
instantiate object,
enter data,
display data,
save data to binary file,
display data,
load data from binary file,
display data,
quit
instantiate object,
enter data,
display data,
save data to binary file,
display data,
load data from binary file,
display data,
quit
So far so good...
2nd Run:
Run program,
instantiate object,
load data from binary file,
display data,
SegFault
instantiate object,
load data from binary file,
display data,
SegFault
Boom, for some reason the program segfaults whenever I try to view the data that was loaded. Any pointers would be greatly appreciated!
The code is below:
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//Globals
class PERSON
{
public:
PERSON() { cout << "\n\tBuilding a person..."; }
~PERSON() { cout << "\n\tDestroying a person..."; }
void SetPersonName(string x) { PersonName = x; }
void SetOccupation(string x) { Occupation = x; }
void SetLocation(string x) { Location = x; }
void SetReferences(string x) { References = x; }
void SetAge(int x) { AGE = x; }
void SetSize(float x) { SIZE = x; }
void SetWeight(double x) { WEIGHT = x; }
string GetPersonName() { return PersonName; }
string GetOccupation() { return Occupation; }
string GetLocation() { return Location; }
string GetReferences() { return References; }
int GetAge() { return AGE; }
float GetSize() { return SIZE; }
double GetWeight() {return WEIGHT; }
private:
string PersonName;
string Occupation;
string Location;
string References;
int AGE;
float SIZE;
double WEIGHT;
};
//Protos----------------------------------------------------------
void CreatePerson();
void EditPerson();
void DisplayPerson();
void SavePerson();
void LoadPerson();
void SavePersonBinary();
void LoadPersonBinary();
PERSON * pPerson;
//-----------------------------------------------------------------
int main()
{
char CHOICE[10]; //Could be single char but this is safer
// pPerson = new PERSON();
cout << "\n\t Personnel Database \n";
while(CHOICE[0] != 'q')
{
cout << "\n\t--------------------Main menu----------------";
cout << "\n\t| |";
cout << "\n\t| (C)reate a person |";
cout << "\n\t| (E)dit a person |";
cout << "\n\t| (D)isplay a person |";
cout << "\n\t| (S)ave a person (Seq) |";
cout << "\n\t| (L)oad a person (Seq) |";
cout << "\n\t| (1)Save a person (Bin) |";
cout << "\n\t| (0)Load a person (Bin) |";
cout << "\n\t| |";
cout << "\n\t---------------------------------------------\n\t";
cin >> CHOICE;
switch(CHOICE[0])
{
case 'c' : CreatePerson(); break;
case 'e' : EditPerson(); break;
case 'd' : DisplayPerson(); break;
case 's' : SavePerson(); break;
case 'l' : LoadPerson(); break;
case '1' : SavePersonBinary(); break;
case '0' : LoadPersonBinary(); break;
case 'q' : cout << "\n\tExiting...\n\n"; break;
}
}
return 0;
}
//-----------------------------------------------------------------
void CreatePerson()
{
pPerson = new PERSON();
}
//-----------------------------------------------------------------
void EditPerson()
{
string TEMP;
char NumBuffer[10];
cout << "\n\t-----------------------Edit Person-----------------";
cout << "\n\tName: ";
cin.ignore();
getline(cin, TEMP);
pPerson->SetPersonName(TEMP);
cout << "\n\tAge: ";
getline(cin, TEMP);
for(int x = 0; x < TEMP.length(); x++)
{ NumBuffer[x] = TEMP[x]; }
pPerson->SetAge(atoi(NumBuffer));
cout << "\n\tSize: ";
getline(cin, TEMP);
for(int x = 0; x < TEMP.length(); x++)
{ NumBuffer[x] = TEMP[x]; }
pPerson->SetSize(atof(NumBuffer));
cout << "\n\tWeight: ";
getline(cin, TEMP);
for(int x = 0; x < TEMP.length(); x++)
{ NumBuffer[x] = TEMP[x]; }
pPerson->SetWeight(atof(NumBuffer));
cout << "\n\tOccupation: ";
getline(cin, TEMP);
pPerson->SetOccupation(TEMP);
cout << "\n\tLocation: ";
getline(cin, TEMP);
pPerson->SetLocation(TEMP);
cout << "\n\tReferences: ";
getline(cin, TEMP);
pPerson->SetReferences(TEMP);
}
//-----------------------------------------------------------------
void DisplayPerson()
{
cout << "\n\t----------Person Information----------";
cout << "\n\tName: " << pPerson->GetPersonName();
cout << "\n\tAge: " << pPerson->GetAge();
cout << "\n\tSize: " << pPerson->GetSize();
cout << "\n\tWeight: " << pPerson->GetWeight();
cout << "\n\tOccupation: " << pPerson->GetOccupation();
cout << "\n\tLocation: " << pPerson->GetLocation();
cout << "\n\tReferences: " << pPerson->GetReferences();
cout << "\n\t--------------------------------------\n\n";
}
//-----------------------------------------------------------------
void SavePerson()
{
try
{
ofstream DATAFILE;
DATAFILE.open("data.bin",ios::out);
DATAFILE << pPerson->GetPersonName() << endl;
DATAFILE << pPerson->GetAge() << endl;
DATAFILE << pPerson->GetSize() << endl;
DATAFILE << pPerson->GetWeight() << endl;
DATAFILE << pPerson->GetOccupation() << endl;
DATAFILE << pPerson->GetLocation() << endl;
DATAFILE << pPerson->GetReferences() << endl;
DATAFILE.close();
cout << "\n\tSuccess! Data was saved to file.";
}
catch(exception x)
{ cout << "\n\tI/O Error! Could not Save Person"; }
}
//-----------------------------------------------------------------
void LoadPerson()
{
try
{
string temp;
char NumBuffer[10];
ifstream DATAFILE;
DATAFILE.open("data.bin", ios::in);
getline(DATAFILE, temp);
pPerson->SetPersonName(temp);
getline(DATAFILE, temp);
for(int x = 0; x < temp.length(); x++)
{ NumBuffer[x] = temp[x]; }
pPerson->SetAge(atoi(NumBuffer));
getline(DATAFILE, temp);
for(int x = 0; x < temp.length(); x++)
{ NumBuffer[x] = temp[x]; }
pPerson->SetSize(atof(NumBuffer));
getline(DATAFILE, temp);
for(int x = 0; x < temp.length(); x++)
{ NumBuffer[x] = temp[x]; }
pPerson->SetWeight(atof(NumBuffer));
getline(DATAFILE, temp);
pPerson->SetOccupation(temp);
getline(DATAFILE, temp);
pPerson->SetLocation(temp);
getline(DATAFILE, temp);
pPerson->SetReferences(temp);
DATAFILE.close();
cout << "\n\tSuccess! Data was loaded.";
}
catch(exception x)
{ cout << "\n\tI/O Error! Unable to load data!"; }
}
//-----------------------------------------------------------------
void SavePersonBinary()
{
try
{
ofstream DATAFILE;
DATAFILE.open("data.bin", ios::out | ios::binary);
cout << "\n\tSize of a pPerson is: " << sizeof(*pPerson) << "\n";
DATAFILE.write((char*) pPerson, sizeof(*pPerson));
DATAFILE.close();
cout << "\n\tOk! Data was saved successfully.";
}
catch(exception x) { cout << "\n\tError! Could not Save!"; }
}
void LoadPersonBinary()
{
try
{
ifstream DATAFILE;
DATAFILE.open("data.bin", ios::in | ios::binary);
cout << "\n\tSize of a pPerson is: " << sizeof(*pPerson) << "\n";
DATAFILE.read((char*) pPerson, sizeof(*pPerson));
DATAFILE.close();
cout << "\n\tSuccess! The data was loaded.";
}
catch(exception x)
{ cout << "\n\tError! Could not load data."; }
}