Chad said:
Okay, I don't know. Maybe this might give you some insight into what
I'm thinking. Say I have something like
fputs("line", file)
We would have 'l' 'i' 'n' 'e' '\0',
The phrase "We would have" is vague.
The string literal "line" in your source specifies that an array
containing the sequence { 'l', 'i', 'n', 'e', '\0' } exists in memory
as your program executes. A pointer to the first element of that
array is passed to the fputs function.
The characters written to the file by the fputs function are 'l', 'i',
'n', and 'e'. fputs does not write either a '\0' or a '\n' to the
file.
When it put into memory (say from fgets), would it have been
'l' 'i' 'n' 'e' '\0'
or
'l' 'i' 'n' 'e' '\0' '\0'
?
You need to be much clearer about what you're doing.
The fgets() function reads a *line* of text from an input file, up to
and including the '\n' character that marks the end of the line. (It
may or may not be represented that way in the external file.)
If a text file starts with 'l', 'i', 'n', 'e', '\n', and you read from
that file using fgets() with a sufficiently long array and a
sufficiently large second argument, then fgets() will store { 'l',
'i', 'n', 'e', '\n', '\0' } in the array.
In some circumstances, fgets won't read a full line. In that case, it
might store just { 'l', 'i', 'n', 'e', '\0' } in the array.
It will *always* store the trailing '\0', ensuring that the array will
contain a valid string.
[snip]
It (almost) *never* makes sense to write a null character '\0' to a
text stream. '\0' is used to mark the end of a string in memory in a
running C program; it has no particular meaning in a text file. (You
can write '\0', or anything else, to a binary file.)