By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,728 Members | 1,154 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,728 IT Pros & Developers. It's quick & easy.

strange read in of binary file

P: n/a
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();
---------------------

Please help!

zl2k
Apr 11 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
zl2k wrote:
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();
---------------------

Please help!
Common mistake. First, you never check the result of your read
operations. Change your code to behave appropriately if the file
cannot be read (where you read your 'num2'): do you really want
to keep reading the stream if the first read fails? Second, I
strongly recommend reading the archives on file streams, 'read',
formatted (or unformatted) I/O, etc. BTW, what book are you
reading that doesn't explain how to handle input that can run
out of information (eof conditions)?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 11 '08 #2

P: n/a
On Apr 11, 3:45 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
zl2k wrote:
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();
---------------------
Please help!

Common mistake. First, you never check the result of your read
operations. Change your code to behave appropriately if the file
cannot be read (where you read your 'num2'): do you really want
to keep reading the stream if the first read fails? Second, I
strongly recommend reading the archives on file streams, 'read',
formatted (or unformatted) I/O, etc. BTW, what book are you
reading that doesn't explain how to handle input that can run
out of information (eof conditions)?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
I tried while (!myfile2.eof()){} but makes no difference. How can I
fix it? I know my code did not stop when reached the end of the file.
zl2k
Apr 11 '08 #3

P: n/a
zl2k wrote:
On Apr 11, 3:45 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>zl2k wrote:
>>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();
---------------------
>>Please help!

Common mistake. First, you never check the result of your read
operations. Change your code to behave appropriately if the file
cannot be read (where you read your 'num2'): do you really want
to keep reading the stream if the first read fails? Second, I
strongly recommend reading the archives on file streams, 'read',
formatted (or unformatted) I/O, etc. BTW, what book are you
reading that doesn't explain how to handle input that can run
out of information (eof conditions)?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

I tried while (!myfile2.eof()){} but makes no difference. How can I
fix it? I know my code did not stop when reached the end of the file.
zl2k
Look at what 'read' returns. You can also check the condition of the
stream right after 'read' to see whether the stream is "good". You
can check for 'eof' instead of "good" after 'read'...

I don't want to write your program for you. Trust me, you'll be much
better off if you research this stuff yourself.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 11 '08 #4

P: n/a
On Apr 11, 4:40 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
zl2k wrote:
On Apr 11, 3:45 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
zl2k wrote:
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();
---------------------
>Please help!
Common mistake. First, you never check the result of your read
operations. Change your code to behave appropriately if the file
cannot be read (where you read your 'num2'): do you really want
to keep reading the stream if the first read fails? Second, I
strongly recommend reading the archives on file streams, 'read',
formatted (or unformatted) I/O, etc. BTW, what book are you
reading that doesn't explain how to handle input that can run
out of information (eof conditions)?
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
I tried while (!myfile2.eof()){} but makes no difference. How can I
fix it? I know my code did not stop when reached the end of the file.
zl2k

Look at what 'read' returns. You can also check the condition of the
stream right after 'read' to see whether the stream is "good". You
can check for 'eof' instead of "good" after 'read'...

I don't want to write your program for you. Trust me, you'll be much
better off if you research this stuff yourself.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Thanks, I can jump out off the loop by checking the good() or eof()
after myfile2.read((char*)num2, sizeof(int));
So I can move one. Have good weekend.
zl2k
Apr 11 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.