So, I just started using Ubuntu recently.
I'm building this very simple program that will let me store some data and read back from it later on.
Using C++, fstream, some objects are stored in .dat file.
// function to store
void storeRecord(Record r){
fstream afile;
afile.open("file.dat" , ios::out | ios::binary | ios::app);
afile.write(reinterpret_cast <const char*> (&r), sizeof(r) );
afile.close();
}
But when I try to (call query() function) read from the same file, I'm getting "Segmentation fault (core dumped)"
void query(){
Record r;
fstream afile;
afile.open("file.dat", ios::in | ios::binary);
while(afile.read(reinterpret_cast <char*> (&r), sizeof(r))){
// do something
}
afile.close();
}
This used to work on windows. Why is that?
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class Record{
public:
// Constructors
Record();
Record(string accountID, string name, string deptID, string password, int role);
~Record();
// Assessors and Mutators
string getAccountID();
string getName();
string getDeptID();
string getPW();
int getRole();
void setAccountID(string accountID);
void setName(string name);
void setDeptID(string deptID);
void setPW(string PW);
void setRole(int role);
// Other functions
string toString();
private:
string accountID;
string name;
string deptID;
string password;
int role; // normal user, HR personal, admin
};
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…