I'm trying to write to a binary file, such that I have time, id1, id2, and two garbage bytes (0) per line. When I do this without the garbage bytes, it works, but once I add the garbage bytes, I get "segmentation fault 11" as an error. I'm sure the CastorArray2
is written correctly (checked before this block of code).
Here is my code:
struct Packet
{
//int64_t Time;
uint32_t Time;
uint32_t Crystal_ID1;``
uint32_t Crystal_ID2;
int Garbage1;
int Garbage2;
};
printf("working before opening file");
ofstream myfile;
myfile.open("/Users/Desktop/Archive/WorkNEW.dat", ios::binary |
ios::out);
if (myfile.is_open()) {
printf("file open");
// cout<< "cannot open file!" << endl;
// return 1;
}
Packet Event[495842];
for (int n=0; n<495842; n++)
{
Event[n].Time=1.0;
// Event[n].Time=CastorArray2[n][3];
Event[n].Crystal_ID1=CastorArray2[n][1];
Event[n].Crystal_ID2=CastorArray2[n][2];
Event[n].Garbage1=0;
Event[n].Garbage2=0;
}
if (myfile.is_open()) {
printf("Works");
// cout<<"file opened successfully"<<endl;
// return 1;
for (int m=0;m<495842;m++)
{
myfile.write((char*) &Event[m], sizeof(Packet));
}
myfile.close();
}
else {
printf("notworking");
//cout<< "unable to open file"<<endl;
return 1;
}
//}
return 0;