Reputation: 15062
If i want to store a Null-terminated string into a file, and the file will only containing that string, is the "\0" or "NULL"-character stored in the file (before the "EOF" (End of File)-sign)?
Furthermore: Is the result depended from the operation system and so on the compiler, on which i will compile the source code on?
Upvotes: 1
Views: 1937
Reputation: 263497
You might be able to write null characters to a text file, but you almost certainly don't want to.
A string (defined as "a contiguous sequence of characters terminated by and including the first null character") is an in-memory data format.
A text stream consists of a sequence of lines:
A text stream is an ordered sequence of characters composed into lines, each line consisting of zero or more characters plus a terminating new-line character. Whether the last line requires a terminating new-line character is implementation-defined.
A string may or may not contain a single line of text. If it represents a line of text, it may or may not include the terminating new-line '\n'
character (you'll need to keep track of that yourself).
If you have a sequence of strings in memory, the usual way to write them to a text file is to write the contents of each string, not including the terminating null character, to the file, adding a new-line character if necessary. Functions like fprintf
and fputs
assume their arguments are strings, so they take care of omitting the '\0'
.
You can write a null character to a text stream, but it's implementation-defined what will actually be written to the file. You can write a null character, or any byte value, to a binary stream -- but then you can't safely use string functions (strlen()
et al, or even fgets()
and fputs()
) on data written to or read from the stream. (And in practice, most systems allow null characters to be written to and read from text files -- though a number of standard library functions assume that text files contain only printable characters.)
Upvotes: 3
Reputation: 60097
'\0'
is not a printing character so if you use an io stream in text mode, then whether it will be preserved when you write it to a file through such a stream is implementation-dependent.
A text stream is an ordered sequence of characters composed into lines, each line consisting of zero or more characters plus a terminating new-line character. Whether the last line requires a terminating new-line character is implementation-defined. Characters may have to be added, altered, or deleted on input and output to conform to differing conventions for representing text in the host environment. Thus, there need not be a one- to-one correspondence between the characters in a stream and those in the external representation. Data read in from a text stream will necessarily compare equal to the data that were earlier written out to that stream only if: the data consist only of printing characters and the control characters horizontal tab and new-line; no new-line character is immediately preceded by space characters; and the last character is a new-line character. Whether space characters that are written out immediately before a new-line character appear when read in is implementation-defined.
If you write the '\0'
to a file through a binary stream (one opened with e.g., fopen("file","wb")
), e.g., with fputc('\0',f)
or fwrite("",1,1,f)
, you should be able to get it back.
Upvotes: 2
Reputation: 308402
No, the functions that write a string to a file will not include the terminating null. You can write a null to a file using a function that takes a byte count, but that doesn't make sense because there's no corresponding read function.
Upvotes: 1