Re: write



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) );
.



Relevant Pages

  • Re: How to convert Infix notation to postfix notation
    ... If this is for an error message, why isn't it using stderr for its output? ... array of 15 characters, and you call this function with the limit 15 on ... Making sure that the only string I allocate and append to, ... because mulFactor in all versions must needs incorporate the functions ...
    (comp.lang.c)
  • Re: Prothon should not borrow Python strings!
    ... """It does not make sense to have a string without knowing what encoding ... same cul de sac as Python. ... Prothon_String_As_ASCII // raises error if there are high characters ... Python's split between byte strings and Unicode strings is ...
    (comp.lang.python)
  • Re: Letter to US Sen. Byron Dorgan re unpaid overtime
    ... put them in stupid places. ... Programming is difficult (as you must surely appreciate, ... > strings will be in the range 1...1000 characters. ... impose an artificially small limit on string length." ...
    (comp.programming)
  • Re: Byte Array to String
    ... retrieved text will mismatch the original characters. ... encoding the characters. ... Dim strFileData as String ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: A note on personal corruption as a result of using C
    ... impossible to write effective string validation routines by definition ... (Note that a string literal may contain embedded null characters; ... without resorting to abusive language. ... In practice, programmers typically use "struct" ...
    (comp.programming)