Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > A question about fputs()

Reply
Thread Tools

A question about fputs()

 
 
Chad
Guest
Posts: n/a
 
      06-22-2008
Why doesn't fputs() write the null byte when the null-terminted string
is written to a stream? I don't see the reasoning behind this.

Chad
 
Reply With Quote
 
 
 
 
Chad
Guest
Posts: n/a
 
      06-22-2008
On Jun 22, 8:58 am, Richard Heathfield <(E-Mail Removed)> wrote:
> Chad said:
>
> > Why doesn't fputs() write the null byte when the null-terminted string
> > is written to a stream? I don't see the reasoning behind this.

>
> Perhaps you could explain why you think a text-oriented function like fputs
> should concern itself with a non-text-oriented value such as a null byte?
> I don't see the reasoning behind /that/.
>


Let's say I have a string like the following

"This is a string\n"

Wouldn't this create undefined behavior if puts removes '\n'?
 
Reply With Quote
 
 
 
 
vippstar@gmail.com
Guest
Posts: n/a
 
      06-22-2008
On Jun 22, 7:03 pm, Chad <(E-Mail Removed)> wrote:
> On Jun 22, 8:58 am, Richard Heathfield <(E-Mail Removed)> wrote:
>
> > Chad said:

>
> > > Why doesn't fputs() write the null byte when the null-terminted string
> > > is written to a stream? I don't see the reasoning behind this.

>
> > Perhaps you could explain why you think a text-oriented function like fputs
> > should concern itself with a non-text-oriented value such as a null byte?
> > I don't see the reasoning behind /that/.

>
> Let's say I have a string like the following
>
> "This is a string\n"
>
> Wouldn't this create undefined behavior if puts removes '\n'?


puts or fputs()?
Regardless, each function *will* write the string provided.
The difference is that puts() does not take the file stream to write
to as an argument, and that puts() also writes a newline, after the
string.
If you really want to know what happened, preserve the return value.
Undefined behavior occurs because of the user-programmer, not the
implementation. (in a perfect world with a perfect implementation).
 
Reply With Quote
 
Chad
Guest
Posts: n/a
 
      06-22-2008
On Jun 22, 9:11 am, Richard Heathfield <(E-Mail Removed)> wrote:
> Chad said:
>
>
>
> > On Jun 22, 8:58 am, Richard Heathfield <(E-Mail Removed)> wrote:
> >> Chad said:

>
> >> > Why doesn't fputs() write thenullbytewhen thenull-terminted string
> >> > is written to a stream? I don't see the reasoning behind this.

>
> >> Perhaps you could explain why you think a text-oriented function like
> >> fputs should concern itself with a non-text-oriented value such as a
> >>nullbyte? I don't see the reasoning behind /that/.

>
> > Let's say I have a string like the following

>
> > "This is a string\n"

>
> > Wouldn't this create undefined behavior if puts removes '\n'?

>
> I can go further than that, and say that it would be printed out backwards
> if puts printed stuff out backwards. But puts doesn't do that - and it
> doesn't remove newlines either. Text is a line-oriented format, and a line
> is defined by newlines (insofar as a newline appears at the end of every
> line). The puts function, which is a text-oriented function, would be a
> very strange bird indeed if it removed newlines. I don't see the point you
> are trying to make. If you are trying to draw a comparison betweennull
> bytes and newlines, it doesn't work - newlines are a fundamental part of
> text streams, whereasnullbytes are not.
>
> --


I'm hesitant to quote the following because it doesn't really fall
under the realm of C. Here is the quote from page 143 in the book
'Advanced Programming in the Unix Environment", Second Edition, by
Stevens and Rago

"The function fputs writes the null-terminated string to the specified
stream. The null byte at the end is not written. Note that this need
not be line-at-a-time-output, since the string need not contain a
newline as the last non-null character. Usually this is the case - the
last non-null character is a newline - but it's not required."



 
Reply With Quote
 
Chad
Guest
Posts: n/a
 
      06-22-2008
On Jun 22, 9:10 am, (E-Mail Removed) wrote:
> On Jun 22, 7:03 pm, Chad <(E-Mail Removed)> wrote:
>
>
>
> > On Jun 22, 8:58 am, Richard Heathfield <(E-Mail Removed)> wrote:

>
> > > Chad said:

>
> > > > Why doesn't fputs() write thenullbytewhen thenull-terminted string
> > > > is written to a stream? I don't see the reasoning behind this.

>
> > > Perhaps you could explain why you think a text-oriented function like fputs
> > > should concern itself with a non-text-oriented value such as anullbyte?
> > > I don't see the reasoning behind /that/.

>
> > Let's say I have a string like the following

>
> > "This is a string\n"

>
> > Wouldn't this create undefined behavior if puts removes '\n'?

>
> puts or fputs()?
> Regardless, each function *will* write the string provided.
> The difference is that puts() does not take the file stream to write
> to as an argument, and that puts() also writes a newline, after the
> string.
> If you really want to know what happened, preserve the return value.
> Undefined behavior occurs because of the user-programmer, not the
> implementation. (in a perfect world with a perfect implementation).



I meant fputs(). Quote the linux man page on fputs()

" fputs() writes the string s to stream, without its trailing \0."

I don't see why it omits the trailing '\0'.
 
Reply With Quote
 
Chad
Guest
Posts: n/a
 
      06-22-2008
On Jun 22, 9:23 am, Joe Wright <(E-Mail Removed)> wrote:
> Chad wrote:
> > Why doesn't fputs() write thenullbyte when thenull-terminted string
> > is written to a stream? I don't see the reasoning behind this.

>
> > Chad

>
> The asciicharacterNUL does not normally appear in a text stream. It is
> used in memory as a 'string' terminator. Text streams are typically
> organized as some number of characters terminated with '\n', a 'line',
> and any number of lines.
>
> The fgets() function will read a line from the text stream, including
> the '\n' into a char array and terminate it with NUL making the whole
> thing a 'string' in memory. Then fputs() can write the array to a text
> stream up to and including the '\n'.
>
> The NUL is a controlcharacterused to define a string in memory. It is
> not part of the data read with fgets() or written with fputs().
>


Okay. I think I see it. I guess I was blurring the distinction between
a 'text string' vs a 'string'.
 
Reply With Quote
 
Raymond Martineau
Guest
Posts: n/a
 
      06-22-2008
On Sun, 22 Jun 2008 09:21:19 -0700 (PDT), Chad <(E-Mail Removed)>
wrote:

>On Jun 22, 9:10 am, (E-Mail Removed) wrote:
>> On Jun 22, 7:03 pm, Chad <(E-Mail Removed)> wrote:
>>


>> > Let's say I have a string like the following

>>
>> > "This is a string\n"

>>
>> > Wouldn't this create undefined behavior if puts removes '\n'?

>>
>> puts or fputs()?
>> Regardless, each function *will* write the string provided.
>> The difference is that puts() does not take the file stream to write
>> to as an argument, and that puts() also writes a newline, after the
>> string.
>> If you really want to know what happened, preserve the return value.
>> Undefined behavior occurs because of the user-programmer, not the
>> implementation. (in a perfect world with a perfect implementation).

>
>
>I meant fputs(). Quote the linux man page on fputs()
>
>" fputs() writes the string s to stream, without its trailing \0."
>
>I don't see why it omits the trailing '\0'.


fputs() cannot tell the difference between a trailing '\0', and the
marker for the end of string (which is '\0'). Standard strings in C
are always null-terminated, and the library functions will treat those
strings in that fashion.

It's similar to why strcat(str, "\0") doesn't do anything - it finds
the '\0' within the string and appends the empty string to that
location.

If you need to write null characters, you could try fputc or a
function dedicated to writing binary data.
 
Reply With Quote
 
Bartc
Guest
Posts: n/a
 
      06-22-2008

"Chad" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Jun 22, 9:23 am, Joe Wright <(E-Mail Removed)> wrote:
>> Chad wrote:
>> > Why doesn't fputs() write thenullbyte when thenull-terminted string
>> > is written to a stream? I don't see the reasoning behind this.

>>
>> > Chad

>>
>> The asciicharacterNUL does not normally appear in a text stream. It is
>> used in memory as a 'string' terminator. Text streams are typically
>> organized as some number of characters terminated with '\n', a 'line',
>> and any number of lines.
>>
>> The fgets() function will read a line from the text stream, including
>> the '\n' into a char array and terminate it with NUL making the whole
>> thing a 'string' in memory. Then fputs() can write the array to a text
>> stream up to and including the '\n'.
>>
>> The NUL is a controlcharacterused to define a string in memory. It is
>> not part of the data read with fgets() or written with fputs().
>>

>
> Okay. I think I see it. I guess I was blurring the distinction between
> a 'text string' vs a 'string'.


I don't think there is a distinction.

puts() writes your string then adds a newline '\n'

fputs() writes your string. It doesn't add a newline of it's own.

The behaviour may be to mirror the action of gets() (which absorbs a newline
on input, so puts() has to regenerate it). And of fgets() (which retains the
newline, and therefore fgets() expects your string to contain a newline, if
you need it).

This is nothing to do with text/non text strings. (Also note gets() function
is not recommended for use anymore, which could be why mixing fgets() and
puts() might give these little problems.)

--
Bartc




 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      06-22-2008
Chad wrote:
> Why doesn't fputs() write the null byte when the null-terminted string
> is written to a stream? I don't see the reasoning behind this.
>
> Chad

By definition, the null marks the location "one past" the end of the
usable string. fputs writes the usable portion of the string.
If you want to save the null, use fputc(0, f); after you write the string.
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      06-22-2008
Chad wrote:
> On Jun 22, 9:11 am, Richard Heathfield <(E-Mail Removed)> wrote:
>> Chad said:
>>
>>
>>
>>> On Jun 22, 8:58 am, Richard Heathfield <(E-Mail Removed)> wrote:
>>>> Chad said:
>>>>> Why doesn't fputs() write thenullbytewhen thenull-terminted string
>>>>> is written to a stream? I don't see the reasoning behind this.
>>>> Perhaps you could explain why you think a text-oriented function like
>>>> fputs should concern itself with a non-text-oriented value such as a
>>>> nullbyte? I don't see the reasoning behind /that/.
>>> Let's say I have a string like the following
>>> "This is a string\n"
>>> Wouldn't this create undefined behavior if puts removes '\n'?

> I'm hesitant to quote the following because it doesn't really fall
> under the realm of C. Here is the quote from page 143 in the book
> 'Advanced Programming in the Unix Environment", Second Edition, by
> Stevens and Rago
>
> "The function fputs writes the null-terminated string to the specified
> stream. The null byte at the end is not written. Note that this need
> not be line-at-a-time-output, since the string need not contain a
> newline as the last non-null character. Usually this is the case - the
> last non-null character is a newline - but it's not required."


Think about this: puts("My line\nand another line\n") will put two lines.

fputs("My line\nand another line\n", file) will also put two lines. \n
doesn't end the string, and puts will put the whole null-terminated
string (minues the null).

Now, puts("My line\n\0and more\n") will only put "My Line\n". Just like
what fputs would do! Why would you expect them to behave differently?



--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments