is there any way to write only the chars in an array and not the terminating /0?

Onceler

Golden Member
Feb 28, 2008
1,262
0
71
I want to write to disk the chars from an array as if they had all been mad as individual chars(not part of an array). Is this possible?
 

nickbits

Diamond Member
Mar 10, 2008
4,122
1
81
You don't need to save the terminating null character to disk. And you can write characters one at a time if you want. The sky's the limit. Post some code if you want a better response.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Using the functions discussed in your other posts it should just be a matter of either not copying the terminating null to the output buffer, or setting the number of bytes to be written so that the last character is ignored. There's nothing special about an array of char in C, which is in fact why the terminating null is necessary.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Did you read my response in your other thread about writing files?
http://forums.anandtech.com/showthread.php?t=2305989

with a function like fwrite you pass it an address and the number of bytes to write. If the string is all single-byte / ASCII characters then the number of bytes is the string length.

But: if it is a variable-length string, how are you going to read it back in later without the 0-terminator?

Say you have 2 strings "hello" and "goodbye". If you use a serialization function it would include the \0 so the file looks like this:

h e l l o \0 g o o d b y e \0

And code to de-serialize can read up to the \0.

If you used fwrite, the file looks like this:

h e l l o g o o d b y e

And a de-serialization function might return "hellogoodbye" the first time you try to read a string.

That's not a problem if all strings are fixed-length, then you can fread using that length and (if needed) add a \0 at [ legnth ] in your array.
 
Last edited:

Onceler

Golden Member
Feb 28, 2008
1,262
0
71
Did you read my response in your other thread about writing files?
http://forums.anandtech.com/showthread.php?t=2305989

with a function like fwrite you pass it an address and the number of bytes to write. If the string is all single-byte / ASCII characters then the number of bytes is the string length.

But: if it is a variable-length string, how are you going to read it back in later without the 0-terminator?

Say you have 2 strings "hello" and "goodbye". If you use a serialization function it would include the \0 so the file looks like this:

h e l l o \0 g o o d b y e \0

And code to de-serialize can read up to the \0.

If you used fwrite, the file looks like this:

h e l l o g o o d b y e

And a de-serialization function might return "hellogoodbye" the first time you try to read a string.

That's not a problem if all strings are fixed-length, then you can fread using that length and (if needed) add a \0 at [ legnth ] in your array.
No I had not read that, something appears to be wrong with my subscriptions-I seem not to be getting notified when someone posts.
Thank you all of you.
 

mv2devnull

Golden Member
Apr 13, 2010
1,519
154
106
No I had not read that, something appears to be wrong with my subscriptions-I seem not to be getting notified when someone posts.
Would it be easier for you, if you could collect all your I/O-related questions into one thread instead of multiple, somewhat redundant ones?

Have you got/found documentation for the I/O-functions (fread, fwrite, ...)?