469,578 Members | 1,263 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,578 developers. It's quick & easy.

strange read in of binary file

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
4 2408
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
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
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
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.

Similar topics

14 posts views Thread by Sebastian Meyer | last post: by
17 posts views Thread by Guyon Morée | last post: by
2 posts views Thread by siggy2 | last post: by
reply views Thread by Grzegorz Kaczor | last post: by
reply views Thread by Grzegorz Kaczor | last post: by
3 posts views Thread by TheSteph | last post: by
reply views Thread by Gwl | last post: by
6 posts views Thread by zl2k | last post: by
6 posts views Thread by =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.