Z
zl2k
hi, there
I have a appendable binary file of complex data structure named
data.bin created by myself. It is written in the following format:
number of Data, Data array
Suppose I have following data.bin (3 Data appended to 2 Data):
2, data0, data1, 3, data0, data1, data2
When I read and display it, I always get the following output:
2
data0
data1
3
data0
data1
data2
3
data with default value
data with default value
data with default value
Why I get an extra data set with default values have the same number
of Data as the previous valid one? Here is the read in code:
---------------------
ifstream myfile2;
myfile2.seekg(0);
myfile2.open ("data.bin", ios::in | ios::binary);
int *num2;
while (myfile2.good()){
myfile2.read((char*)num2, sizeof(int));
cout<<*num2<<" "<<endl;
Data *dataArray2 = new Data[*num2];
myfile2.read ((char*)dataArray2, sizeof (Data) * *num2);
//print dataArray2 content
delete [] dataArray2;
}
myfile2.close();
I have a appendable binary file of complex data structure named
data.bin created by myself. It is written in the following format:
number of Data, Data array
Suppose I have following data.bin (3 Data appended to 2 Data):
2, data0, data1, 3, data0, data1, data2
When I read and display it, I always get the following output:
2
data0
data1
3
data0
data1
data2
3
data with default value
data with default value
data with default value
Why I get an extra data set with default values have the same number
of Data as the previous valid one? Here is the read in code:
---------------------
ifstream myfile2;
myfile2.seekg(0);
myfile2.open ("data.bin", ios::in | ios::binary);
int *num2;
while (myfile2.good()){
myfile2.read((char*)num2, sizeof(int));
cout<<*num2<<" "<<endl;
Data *dataArray2 = new Data[*num2];
myfile2.read ((char*)dataArray2, sizeof (Data) * *num2);
//print dataArray2 content
delete [] dataArray2;
}
myfile2.close();