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)
>
>> 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);
It looks like it'd be easier for me to just use putc and send out one
character at a time, since the data will vary in length and it saves a
step
in not having to put the data into a buffer before writing.
I'm having trouble telling, from the reference, looks like the difference
between putc and fputc is that I can use a char in putc and have to use an
int for fputc (which I can get by casting an char). Are there other
differences?
Thanks!
Hal


|