473,396 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 2752
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Sebastian Meyer | last post by:
Hi newsgroup, i am trying to replace german special characters in strings like str = re.sub('ö', 'oe', str) When i work with this, i always get the message UniCode Error: ASCII decoding error...
17
by: Guyon Morée | last post by:
what is the difference? if I open a text file in binary (rb) mode, it doesn't matter... the read() output is the same.
2
by: siggy2 | last post by:
Hi, I'm using Python 2.3.4 (#53, May 25 2004, 21:17:02) on win32 I've noticed a strange (= not deterministic) behaviour of ftplib.py: sometimes (not always) it fails (after a variable number of...
0
by: Grzegorz Kaczor | last post by:
Hello all, I've got a VERY strange network problem with Win2k Server and .NET. I've got one central server (hub) getting raw binary data (files) from many locations. Both server and clients...
6
by: leonecla | last post by:
Hi everybody, I'm facing a very very strange problem with a very very simple C program... My goal should be to write to a binary file some numbers (integers), each one represented as a sequence...
0
by: Grzegorz Kaczor | last post by:
Hello, I've got a VERY strange network problem with Win2k Server and .NET. I've got one central server (hub) getting raw binary data (files) from many locations. Both server and clients are...
3
by: TheSteph | last post by:
Hi, I have a small serializable struct : public struct TestStruct { public string Title; public int Age; public string Name;
0
by: Gwl | last post by:
I made some test to mesure the c# read perfomance on binary file and I made some curious discovery. Except for some minor details, the following is the code I used to read the file: byte buffer...
6
by: zl2k | last post by:
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...
6
by: =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= | last post by:
Yesterday Visual Studio gave me a strange error both at compiletime and at designtime that had no obvious connection to anything I had changed recently. After some effort tracking down the problem...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.