473,405 Members | 2,404 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,405 software developers and data experts.

How to read null characters in a file

Hi
I want to read a file which has data and null characters in
between...
But my program terminates on encountering NULL characters...is there
any other way to read the whole file with the NULL characters..

thanks in advance...
regards
asvini

Dec 23 '06 #1
7 8177
On 22 Dec 2006 19:55:26 -0800, "asvini" <as******@gmail.comwrote in
comp.lang.c:
Hi
I want to read a file which has data and null characters in
between...
But my program terminates on encountering NULL characters...is there
any other way to read the whole file with the NULL characters..
There are null characters in C, but not NULL characters.

If you want your program to be able to read null characters and it
does not, you have not written it correctly.

Unless you post the code that does not work correctly, nobody can tell
you how to fix it so it does work correctly.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Dec 23 '06 #2

"asvini" <as******@gmail.comwrote in message
news:11**********************@73g2000cwn.googlegro ups.com...
Hi
I want to read a file which has data and null characters in
between...
But my program terminates on encountering NULL characters...is there
any other way to read the whole file with the NULL characters..
Open the file in binary mode by passing "rb" to fopen().
The fgetc() will return 0 on a null character, and -1 or EOF on end of
input.

It is just possible that text mode is interpreting the zero bytes badly,
however more likely that there is something wrong with the program's logic.
Make sure the input isn't being treated as one big string, it which case the
stdlib functions will treat it as terminated after the first null.
--
www.personal.leeds.ac.uk/~bgy1mm
freeware games to download.
Dec 23 '06 #3
"asvini" <as******@gmail.comwrites:
Hi
I want to read a file which has data and null characters in
between...
But my program terminates on encountering NULL characters...is there
any other way to read the whole file with the NULL characters..
There is a way to read a file containing null characters (not NULL
characters). You are probably using the wrong functions for the job.

Use fopen and fread.

Google will reveal more. There can be platform specific issues so refer
to your systems man pages/documentation.

good luck!
>
thanks in advance...
regards
asvini
--
Dec 24 '06 #4
Richard <rg****@gmail.comwrites:
"asvini" <as******@gmail.comwrites:
> I want to read a file which has data and null characters in
between...
But my program terminates on encountering NULL characters...is there
any other way to read the whole file with the NULL characters..

There is a way to read a file containing null characters (not NULL
characters). You are probably using the wrong functions for the job.

Use fopen and fread.
You can also use fgetc(); fread() is defined in terms of repeated
calls to fgetc().
Google will reveal more. There can be platform specific issues so refer
to your systems man pages/documentation.
The only platform specific issue I can think of is that binary files
can be padded at the end with zero bytes (but most implementations
don't do this). Other than that, if you write a sequence of bytes to
a file in binary mode, you'll get the same sequence of bytes back when
you read it (unless, of course, the file has been changed in the
meantime).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 24 '06 #5
On Sun, 24 Dec 2006 11:13:02 -0800, Keith Thompson <ks***@mib.org>
wrote:
>Richard <rg****@gmail.comwrites:
>"asvini" <as******@gmail.comwrites:
>> I want to read a file which has data and null characters in
between...
But my program terminates on encountering NULL characters...is there
any other way to read the whole file with the NULL characters..

There is a way to read a file containing null characters (not NULL
characters). You are probably using the wrong functions for the job.

Use fopen and fread.

You can also use fgetc(); fread() is defined in terms of repeated
calls to fgetc().
I don't see anywhere in the standard where it says that fread() is
defined in terms of repeated calls to fgetc(). That would be very
inefficient on some implementations when reading a "large" amount of
bytes.

--
jay
Dec 25 '06 #6
jaysome schrieb:
On Sun, 24 Dec 2006 11:13:02 -0800, Keith Thompson <ks***@mib.org>
wrote:
>You can also use fgetc(); fread() is defined in terms of repeated
calls to fgetc().

I don't see anywhere in the standard where it says that fread() is
defined in terms of repeated calls to fgetc(). That would be very
inefficient on some implementations when reading a "large" amount of
bytes.
n1124.pdf, 7.19.8.1p2

The fread function reads, into the array pointed to by ptr, up to
nmemb elements whose size is specified by size, from the stream
pointed to by stream. For each object, size calls are made to the
fgetc function and the results stored, in the order read, in an
array of unsigned char exactly overlaying the object. The file
position indicator for the stream (if defined) is advanced by the
number of characters successfully read. If an error occurs, the
resulting value of the file position indicator for the stream is
indeterminate. If a partial element is read, its value is
indeterminate.

fread() just has to behave like repeated calls to fgetc(), but may be
implemented otherwise (as-if clause).
Dec 25 '06 #7

jaysome wrote:
On Sun, 24 Dec 2006 11:13:02 -0800, Keith Thompson <ks***@mib.org>
wrote:
Richard <rg****@gmail.comwrites:
"asvini" <as******@gmail.comwrites:
I want to read a file which has data and null characters in
between...
But my program terminates on encountering NULL characters...is there
any other way to read the whole file with the NULL characters..

There is a way to read a file containing null characters (not NULL
characters). You are probably using the wrong functions for the job.

Use fopen and fread.
You can also use fgetc(); fread() is defined in terms of repeated
calls to fgetc().

I don't see anywhere in the standard where it says that fread() is
defined in terms of repeated calls to fgetc(). That would be very
inefficient on some implementations when reading a "large" amount of
bytes.
No. It should behave *as if*, it's a sequence of fgetc() calls, but the
actual details can vary.

Dec 25 '06 #8

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

Similar topics

3
by: Anne-Marte | last post by:
Hi I simply don't understand how to read a simple file using std::istream. How do I open a file for reading with istream?? Anne-Marte
40
by: Abby | last post by:
My .dat file will contain information like below. /////////// First 0x04 0x05 0x06 Second 0x07
14
by: spike | last post by:
Im trying to write a program that should read through a binary file searching for the character sequence "\name\" Then it should read the characters following the "\name\" sequence until a NULL...
19
by: Mark Richards | last post by:
I've been programming for many years, but have only recently taken a deep "C" dive (bad pun, i know) and need a lot of explanation from an expert. My questions center around those mysterious...
2
by: Profetas | last post by:
I have the following code that detects a <c> and </c> #include <stdio.h> main(int argc, char *argv) { FILE* fp; char data;
8
by: collinm | last post by:
hi we use linux and c language under bash i do echo -e \\000\\000\\000\\000\000\\001Z00\\002AA LINUX \\004 >/dev/ttyS2 that send command to a led display (alpha sign communication)
7
by: Naren | last post by:
Hello All, Can any one help me in this file read problem. #include <stdio.h> int main() {
9
by: Adi | last post by:
Hello eveyone, I wanna ask a very simple question here (as it was quite disturbing me for a long time.) My problem is to read a file line by line. I've tried following implementations but still...
3
by: asvini | last post by:
Hi I want to read a file which has data and null characters in between... But my program terminates on encountering NULL characters...is there any other way to read the whole file with the NULL...
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
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
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...

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.