473,473 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Reading bytes from binary files containing '0' character

Hi,
I am writing this application in C++.
It reads data from binary files.
My current requirement is that: Given a positive number N, I have to
read in N bytes from the input stream (which is from a binary file ).

My code fragment looks as follows:

void MyStream ::readStream(char * buffer, size_t length) {
is.get( buffer, length + 1); //read length bytes from the stream
// 'is' -> field of type istream .

}

The invocation fragment looks as follows:

char * buffer = new char[length + 1];
readStream(buffer, length);
The problem occurs when the binary stream contains a null character in
it.
Suppose, if I try to read 10 characters from the stream, and the fourth
character happens to be a null character (i.e 0 ) , then only 3
characters
are read. I am looking for a work-around to read 10 characters i.e read
past this null character here. Is there any workaround to this ?

Jul 22 '05 #1
2 2689
The problem in your code is not caused by the stream but by the C-style string.
The "get" function does read "length" byte of data into the buffer if the input
stream doesnot meet any error or arrive at the end of file during the read operation.
When a null charactor occurs in your C-style string, it just look like to be
truncated. You just look the string to be a byte array.

"Rakesh Sinha" <ra***********@yahoo.com> wrote in message news:11**********************@z14g2000cwz.googlegr oups.com...
Hi,
I am writing this application in C++.
It reads data from binary files.
My current requirement is that: Given a positive number N, I have to
read in N bytes from the input stream (which is from a binary file ).

My code fragment looks as follows:

void MyStream ::readStream(char * buffer, size_t length) {
is.get( buffer, length + 1); file://read length bytes from the stream
// 'is' -> field of type istream .

}

The invocation fragment looks as follows:

char * buffer = new char[length + 1];
readStream(buffer, length);
The problem occurs when the binary stream contains a null character in
it.
Suppose, if I try to read 10 characters from the stream, and the fourth
character happens to be a null character (i.e 0 ) , then only 3
characters
are read. I am looking for a work-around to read 10 characters i.e read
past this null character here. Is there any workaround to this ?

Jul 22 '05 #2
Rakesh Sinha wrote:
Hi,
I am writing this application in C++.
It reads data from binary files.
My current requirement is that: Given a positive number N, I have to
read in N bytes from the input stream (which is from a binary file ).

My code fragment looks as follows:

void MyStream ::readStream(char * buffer, size_t length) {
is.get( buffer, length + 1); //read length bytes from the stream
// 'is' -> field of type istream .

}

The invocation fragment looks as follows:

char * buffer = new char[length + 1];
readStream(buffer, length);
The problem occurs when the binary stream contains a null character in
it.
Suppose, if I try to read 10 characters from the stream, and the fourth
character happens to be a null character (i.e 0 ) , then only 3
characters
are read. I am looking for a work-around to read 10 characters i.e read
past this null character here. Is there any workaround to this ?


Use std::istream::read().
Jonathan
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: christos panagiotou | last post by:
hi all I am trying to open some .raw files that represent images (256x256, 8 bit per pixel, no header) in a c++ program I cannot copy paste the module here as it uses a method from the VTK...
2
by: jimmyfishbean | last post by:
Hi, I am using VB6, SAX (implementing IVBSAXContentHandler). I need to extract binary encoded data (images) from large XML files and decode this data and generate the appropriate images onto...
20
by: ishmael4 | last post by:
hello everyone! i have a problem with reading from binary file. i was googling and searching, but i just cant understand, why isnt this code working. i could use any help. here's the source code:...
50
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem...
9
by: jeff M via .NET 247 | last post by:
I'm still having problems reading EBCDIC files. Currently itlooks like the lower range (0 to 127) is working. I have triedthe following code pages 20284, 20924, 1140, 37, 500 and 20127.By working I...
30
by: siliconwafer | last post by:
Hi All, I want to know tht how can one Stop reading a file in C (e.g a Hex file)with no 'EOF'?
11
by: Freddy Coal | last post by:
Hi, I'm trying to read a binary file of 2411 Bytes, I would like load all the file in a String. I make this function for make that: '-------------------------- Public Shared Function...
1
by: Wx | last post by:
Hello. I'm trying to read a textfile written by the NTBackup utility on Windows 2003 SBS. The problem is that when i print the output, it looks like this: S t a t o : b a c k u p O p e r a...
6
by: jcasique.torres | last post by:
Hi everyboy. I trying to create a C promang in an AIX System to read JPG files but when it read just the first 4 bytes when it found a DLE character (^P) doesn't read anymore. I using fread...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
1
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...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
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.