By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,440 Members | 1,420 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,440 IT Pros & Developers. It's quick & easy.

How to read null characters in a file

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a

"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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

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 discussion thread is closed

Replies have been disabled for this discussion.