473,881 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8216
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.l earn.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.goog legroups.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.c omwrites:
"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_Keit h) 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.or g>
wrote:
>Richard <rg****@gmail.c omwrites:
>"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.or g>
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.or g>
wrote:
Richard <rg****@gmail.c omwrites:
"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
6090
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
61246
by: Abby | last post by:
My .dat file will contain information like below. /////////// First 0x04 0x05 0x06 Second 0x07
14
3783
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 character is encountered. But when my program runs it gets a SIGSEGV (Segmentation vioalation) signal. Whats wrong? And is there a better way than mine to solve this task (most likely)
19
2111
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 pointer beasties. The following code is excerpted from digitemp, a program that reads 1-wire devices. Digitemp is the work of Brian C. Lane. As I walk through this code, I have a number of questions. I'm hoping someone with experience in...
2
2369
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
14787
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
2273
by: Naren | last post by:
Hello All, Can any one help me in this file read problem. #include <stdio.h> int main() {
9
5219
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 facing problems: Assume that FILE* filePointer; unsigned char lineBuffer;
3
386
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 characters.. thanks in advance... regards asvini
0
9929
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9776
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11102
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
7953
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5781
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5978
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4597
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
2
4196
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3225
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.