473,545 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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((c har*)num2, sizeof(int));
cout<<*num2<<" "<<endl;
Data *dataArray2 = new Data[*num2];
myfile2.read ((char*)dataArr ay2, sizeof (Data) * *num2);
//print dataArray2 content
delete [] dataArray2;
}
myfile2.close() ;
---------------------

Please help!

zl2k
Apr 11 '08 #1
4 2773
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((c har*)num2, sizeof(int));
cout<<*num2<<" "<<endl;
Data *dataArray2 = new Data[*num2];
myfile2.read ((char*)dataArr ay2, 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...@com Acast.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((c har*)num2, sizeof(int));
cout<<*num2<<" "<<endl;
Data *dataArray2 = new Data[*num2];
myfile2.read ((char*)dataArr ay2, 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...@com Acast.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.ope n ("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.rea d ((char*)dataArr ay2, 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...@com Acast.netwrote:
zl2k wrote:
On Apr 11, 3:45 pm, "Victor Bazarov" <v.Abaza...@com Acast.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*)dataArr ay2, 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((c har*)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
3515
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 : ordinal not in range(128) Yes i have googled, i searched the faq, manual and python library and searched all known soruces of information. I...
17
10460
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
2226
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 minutes from 15 to 130) downloading a 150 MB BINARY file (a big gzipped ascii file) with the traceback reported below. IMVHO this is not a...
0
1523
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 are written in C# The server is quite simple: two threads, one accepts new connections and decides whether the client is authenticated to send data or...
6
8507
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 of 32 bit. I made this stupid trial code: --------------------------------------------- FILE *fout;
0
1238
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 written in C# The server is quite simple: two threads, one accepts new connections and decides whether the client is authenticated to send data or...
3
2205
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
1191
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 = new byte; FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.None, fileStreamBufferSize, fileOptions);...
6
342
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 following data.bin (3 Data appended to 2 Data): 2, data0, data1, 3, data0, data1, data2
6
3323
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 I discovered first a workaround, then the real cause of the problem. I would like to understand why what I am doing is frowned upon by Visual Studio...
0
7410
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7437
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7773
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5984
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5343
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3466
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1901
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1025
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.