Talk About Network

Google





Programming > C - C++ Learning > Re: If NULL End...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 19 Topic 4067 of 4400
Post > Topic >>

Re: If NULL Ends a String or Char[], How to Send NULL to a File?

by Hal Vaughan <hal@[EMAIL PROTECTED] > Mar 13, 2008 at 01:38 PM

Richard Heathfield wrote:

> Hal Vaughan said:
> 
>> I have a series of codes that will include NULL bytes
> 
> Shhhhh, we're trying to sleep... NULL is a pointer. You mean null bytes,
> right? i.e. '\0' rather than ((void *)0)

My bad.  Many, many years ago (in the 1980s) I used to program in
Assembler
(VAX 11/780 and 6502) and then didn't program for over a decade, then used
basically Perl and Java.  I'm still getting used to thinking more like
Assembler than like higher level languages.  It takes a while to adjust.

>> and other non-text
>> data that I need to send to a device through a file.
> 
> Not a problem.
> 
>> Since a NULL terminates a string, how do I send a string containing
NULL
>> bytes to a file (or device, depending on the OS)?
> 
> Don't use strings. For example, here's one way to send the bytes 00, 9E,
> 3A, 00, 00, 5F, CD, and FE to a file:
> 
> unsigned char buf[8] = { 0x00, 0x9E, 0x3A, 0x00, 0x00, 0x5F, 0xCD, 0xFE
};
> fwrite(buf, sizeof buf / sizeof buf[0], sizeof buf[0], fp);

Okay, got it.  Just to be sure, fp is filepointer, the same as an int file
descriptor, right?  (I know there are different ways of opening files and
I'm still getting used to this and realize there's a lot I don't know.)

> Now let's send eight straight zeros to the same stream:
> 
> memset(buf, 0, sizeof buf / sizeof buf[0]);
> fwrite(buf, sizeof buf / sizeof buf[0], sizeof buf[0], fp);
> 
> Now let's send a zero and three letter 'N's:
> 
> buf[0] = '\0'; /* not really necessary since we did it a couple of lines
> up */
> buf[1] = buf[2] = buf[3] = 'N';
> fwrite(buf, 4, sizeof buf[0], fp);
> 
> See? It's easy really. Just stop thinking in terms of strings, and think
> in terms of raw data instead.

Okay.  Like I said, I am trying to get used to thinking more like I used
to
many, many eons ago when I used Assembler.

Thanks for the help!

Hal
 




 19 Posts in Topic:
If NULL Ends a String or Char[], How to Send NULL to a File?
Hal Vaughan <hal@[EMAI  2008-03-13 08:28:33 
Re: If NULL Ends a String or Char[], How to Send NULL to a File?
Richard Heathfield <rj  2008-03-13 10:33:41 
Re: If NULL Ends a String or Char[], How to Send NULL to a File?
Hal Vaughan <hal@[EMAI  2008-03-13 13:38:19 
Re: If NULL Ends a String or Char[], How to Send NULL to a File?
Richard Heathfield <rj  2008-03-13 14:00:08 
Re: If NULL Ends a String or Char[], How to Send NULL to a File?
Hal Vaughan <hal@[EMAI  2008-03-13 14:27:55 
Re: If NULL Ends a String or Char[], How to Send NULL to a File?
Richard Heathfield <rj  2008-03-13 14:35:06 
Re: If NULL Ends a String or Char[], How to Send NULL to a File?
Hal Vaughan <hal@[EMAI  2008-03-13 14:38:36 
Re: If NULL Ends a String or Char[], How to Send NULL to a File?
Richard Heathfield <rj  2008-03-13 16:14:14 
Re: If NULL Ends a String or Char[], How to Send NULL to a File?
Hal Vaughan <hal@[EMAI  2008-03-13 16:47:31 
Re: If NULL Ends a String or Char[], How to Send NULL to a File?
"R. Scott Mellow&quo  2008-03-13 13:07:37 
Re: If NULL Ends a String or Char[], How to Send NULL to a File?
Hal Vaughan <hal@[EMAI  2008-03-13 18:59:08 
Re: If NULL Ends a String or Char[], How to Send NULL to a File?
"Ralph D. Ungermann&  2008-03-13 15:03:06 
Re: If NULL Ends a String or Char[], How to Send NULL to a File?
Hal Vaughan <hal@[EMAI  2008-03-13 16:56:49 
Re: If NULL Ends a String or Char[], How to Send NULL to a File?
Richard Heathfield <rj  2008-03-13 17:04:36 
Re: If NULL Ends a String or Char[], How to Send NULL to a File?
Anand Hariharan <mailt  2008-03-13 15:45:18 
Re: If NULL Ends a String or Char[], How to Send NULL to a File?
"Ralph D. Ungermann&  2008-03-13 14:38:28 
Re: If NULL Ends a String or Char[], How to Send NULL to a File?
Philip Potter <pgp@[EM  2008-03-13 16:20:55 
Re: If NULL Ends a String or Char[], How to Send NULL to a File?
"Ralph D. Ungermann&  2008-03-13 18:57:23 
Re: If NULL Ends a String or Char[], How to Send NULL to a File?
"Ed Norris" <  2008-03-15 20:05:55 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
localhost-V2008-12-19 Wed Jan 7 12:01:41 PST 2009.