Re: write
- From: "Wayne C. Morris" <wayne.morris@xxxxxxxxxxxxxxx>
- Date: Tue, 29 Apr 2008 17:19:11 GMT
In article <7WGRj.3423$Q06.749@trnddc03>,
"Bill Cunningham" <nospam@xxxxxxxxx> wrote:
"Joe Pfeiffer" <pfeiffer@xxxxxxxxxxx> wrote in message
news:1b7iehp9ri.fsf@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Neither. Why would the length of a string have a character added to
it?
I guess I've been being told that strlen doesn't recognize the \0 at the
end of a string.
That is correct. strlen doesn't count the '\0' at the end of a string.
strlen("Hello") returns 5.
Some have told me to use strlen(string)+1.
That depends on whether you want the '\0' to be included in the data
being written. If you're writing to a text file, you don't want '\0'
characters in the file, so you would just use strlen(x). In other
situations you might want the '\0' to be included, so you would use
strlen(x)+1.
Text files should contain '\n' characters to mark the end of each line.
You have to write those by either making them part of the string you're
writing, or calling write a second time to write the '\n'.
Example 1:
char string[] = "Hello World\n";
write( file, string, strlen(string) );
Example 2:
char string[] = "Hello World";
write( file, string, strlen(string) );
write( file, "\n", 1 );
Example 3:
char string1[] = "Hello ";
char string2[] = "World\n";
write( file, string1, strlen(string1) );
write( file, string2, strlen(string2) );
.
- References:
- Prev by Date: Re: write
- Next by Date: Re: setrlimit() extension suggestion
- Previous by thread: Re: write
- Next by thread: Re: write
- Index(es):
Relevant Pages
|