Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


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

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

by "Ralph D. Ungermann" <usenet@[EMAIL PROTECTED] > Mar 13, 2008 at 02:38 PM

Hal Vaughan wrote:
> I have a series of codes that will include NULL bytes and other 
> non-text data that I need to send to a device through a file.

> Since a NULL terminates a string

Nope. But there is the concept of "Null-Terminated Byte Strings" (NTBS):
many functions treat a '\0' as the end of the string.

> Do I have to handle the string (or char[]) in a special way to make 
> sure all the data is kept intact?

You must not use any of these NTBS functions, that's all.

In C, an (un)signed char[] should be fine, and ordinary array
initialization will work as expected:
   unsigned char dev_out[6] = { '\0x03', '\0', 0, 255, 'x', '\r' };
I'm not aware of any sup****t for NUL in string literals, e.g.
"abc\0def"; I'd just avoid it.
Of course, you cannot use strlen() or strcpy() on such objects.

In C++, a std::string _can_ handle embedded NUL characters, but there
are also many convenient functions for NUL-terminated strings, which are
useless and dangerous for your purpose. And as you said you have "a
series of codes", using a std::string just sounds unnatural to me.
A std::vector<unsigned char> is probably a better choice, and won't
cause you any trouble.

Last not least, make sure to open the device in binary mode, and use
only appropriate output functions.

-- ralph
 




 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
tan12V112 Fri Sep 5 2:53:19 CDT 2008.