Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > which functions set the end-of-file indicator?

Reply
Thread Tools

which functions set the end-of-file indicator?

 
 
nicolas.sitbon@gmail.com
Guest
Posts: n/a
 
      08-04-2008
Hi everybody, in a french C book, the author says that only {fgetc,
getc, getchar, fgetwc, getwc, getwchar, fgets, gets, fgetws, getws,
fputc, putc, putchar, fputwc, putwc, putwchar, fputs, puts, fputws}
are guaranteed to set the end-of-file indicator when the end-of-file
is reached, but in C99 standard, I find p 288 (ISO/IEC 9899:TC3
Committee Draft — Septermber 7, 2007 WG14/N1256)

/* ============================= */
#include <stdio.h>
/* ... */
int count; float quant; char units[21], item[21];
do {
count = fscanf(stdin, "%f%20s of %20s", &quant, units, item);
fscanf(stdin,"%*[^\n]");
} while (!feof(stdin) && !ferror(stdin));
/* ============================= */
It seems to say that fscanf family function set end-of-file indicator
too? which functions set the end-of-file indicator?
 
Reply With Quote
 
 
 
 
vippstar@gmail.com
Guest
Posts: n/a
 
      08-04-2008
On Aug 4, 9:05 pm, nicolas.sit...@gmail.com wrote:
<snip>

> It seems to say that fscanf family function set end-of-file indicator
> too? which functions set the end-of-file indicator?


All functions that perform IO operations. (ie even perror() can set
the 'error' indicator for stderr if an error occurs at writing)
 
Reply With Quote
 
 
 
 
Chris Torek
Guest
Posts: n/a
 
      08-04-2008
In article <27d81185-ca59-482e-898a->
<> wrote:
>Hi everybody, in a french C book, the author says that only {fgetc,
>getc, getchar, fgetwc, getwc, getwchar, fgets, gets, fgetws, getws,
>fputc, putc, putchar, fputwc, putwc, putwchar, fputs, puts, fputws}
>are guaranteed to set the end-of-file indicator when the end-of-file
>is reached ...


This is incorrect. In the "obviously wrong" part, putc() is not
going to set the end-of-file indicator, for instance. But it
is still wrong, and perhaps overly complicated.

All I/O is done "as if" via fgetc() and fputc(). If fgetc() would
have set the end-of-file indicator, any other input-oriented
operation that would have called fgetc() must also set the e-o-f
indicator. (And of course, if fgetc() would have set the error
indicator, any other input-oriented operation that would have called
fgetc() must also set the error indicator. The "would have"s here
simply mean that, deep inside the implementation, scanf() or fread()
or whatever might use something "faster" or "better" than an actual
call to fgetc(), such as an inline expansion of what fgetc() does.
But if they do avoid fgetc() in favor of something "better", they
still have to act *as if* they had made an actual call to fgetc(),
including setting the e-of-f and/or error indicators in the same
way that fgetc() would have.)

(For instance, an implementation might have a macro like this:

#define fast_inlined_getc(f) \
((f)->_readcount > 0 ? --(f)->_readcount, *(f)->_readptr++ : \
fgetc(f))

and it could then use this in the scanf engine. The fgets() and
fread() functions might look directly at f->_readcount:

int fgets(char *buf, size_t bufsize, FILE *stream) {
...
p = memchr(f->_readptr, '\n', min(f->_readsize, room_in_user_buffer));
if (p != NULL) {
/*
* Found entire input line in current buffer. Do a quick
* copy-and-take.
*/
line_len = p - f->_readptr; /* length of the line */
memcpy(buf, f->_readptr, line_len); /* copy it */
f->_readptr += line_len; /* and take it out */
f->_readsize -= line_len; /* of the stdio buffer */
} else {
/*
* Found only part of line in current buffer, or buffer
* is empty. Do whatever is required to copy partial
* line (if any) and read more input, in a loop as needed.
*/
...
}
...
}

At some point, though, these all call fgetc() (or the underlying
code that implements fgetc()) to refill the stdio buffer. It is
this code that handles "read from underlying file or device"
requests, and it is this "read from underlying file or device"
request that encounters the kinds of failures that set the end-of-file
and/or error indicators.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: gmail (figure it out) http://web.torek.net/torek/index.html
 
Reply With Quote
 
nicolas.sitbon@gmail.com
Guest
Posts: n/a
 
      08-04-2008
OK OK, everybody seems to be ok to say that all I/O functions can
potentially set/clear the end-of-file indicator, but nobody give me
normative reference, and the author says that nothing in the C99 says
that all I/O functions can set/clear end-of-file indicator!!!
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      08-04-2008
wrote:

> OK OK, everybody seems to be ok to say that all I/O functions can
> potentially set/clear the end-of-file indicator, but nobody give me
> normative reference, and the author says that nothing in the C99 says
> that all I/O functions can set/clear end-of-file indicator!!!


Here you go:

<http://www.iso.org/iso/iso_catalogue/catalogue_tc/catalogue_detail.htm?csnumber=29237>

 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      08-04-2008
Eric Sosman wrote:

> wrote:
>> OK OK, everybody seems to be ok to say that all I/O functions can
>> potentially set/clear the end-of-file indicator, but nobody give me
>> normative reference, and the author says that nothing in the C99 says
>> that all I/O functions can set/clear end-of-file indicator!!!

>
> Unless the post hasn't reached my news server yet, nobody has
> claimed that all I/O functions can set or clear the end-of-file
> indicator. putc(), for example, cannot. Nor can ftell(), nor
> setvbuf(), nor feof(), nor a bunch of others.


To quote vippstar:

All functions that perform IO operations. (ie even perror() can set
the 'error' indicator for stderr if an error occurs at writing)

A bit of an overstatement, but this happens in ordinary speech. We can't
all expect be like Keith!

<snip>

 
Reply With Quote
 
nicolas.sitbon@gmail.com
Guest
Posts: n/a
 
      08-04-2008
On 4 août, 21:30, Eric Sosman <Eric.Sos...@sun.com> wrote:
> nicolas.sit...@gmail.com wrote:
> > OK OK, everybody seems to be ok to say that all I/O functions can
> > potentially set/clear the end-of-file indicator, but nobody give me
> > normative reference, and the author says that nothing in the C99 says
> > that all I/O functions can set/clear end-of-file indicator!!!

>
>
> * * *The normative reference is section 7.19 of the Standard.
>
> --
> Eric.Sos...@sun.com

I looked at this section, but I didn't find anything saying clearly
that all IO input functions set/clear the end-of-file indicator!
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      08-04-2008
wrote:

> On 4 août, 21:30, Eric Sosman <Eric.Sos...@sun.com> wrote:
>> nicolas.sit...@gmail.com wrote:
>> > OK OK, everybody seems to be ok to say that all I/O functions can
>> > potentially set/clear the end-of-file indicator, but nobody give me
>> > normative reference, and the author says that nothing in the C99
>> > says that all I/O functions can set/clear end-of-file indicator!!!

>>
>>
>> The normative reference is section 7.19 of the Standard.


> I looked at this section, but I didn't find anything saying clearly
> that all IO input functions set/clear the end-of-file indicator!


Hasn't it been made clear to you by now that only one poster has said
that all functions that perform an I/O operation set these flags, and
that he was overstating his case, as has been explained more than once?

 
Reply With Quote
 
nicolas.sitbon@gmail.com
Guest
Posts: n/a
 
      08-04-2008
On 4 août, 21:59, santosh <santosh....@gmail.com> wrote:
> nicolas.sit...@gmail.com wrote:
> > On 4 août, 21:30, Eric Sosman <Eric.Sos...@sun.com> wrote:
> >> nicolas.sit...@gmail.com wrote:
> >> > OK OK, everybody seems to be ok to say that all I/O functions can
> >> > potentially set/clear the end-of-file indicator, but nobody give me
> >> > normative reference, and the author says that nothing in the C99
> >> > says that all I/O functions can set/clear end-of-file indicator!!!

>
> >> The normative reference is section 7.19 of the Standard.

> > I looked at this section, but I didn't find anything saying clearly
> > that all IO INPUT functions set/clear the end-of-file indicator!

>
> Hasn't it been made clear to you by now that only one poster has said
> that all functions that perform an I/O operation set these flags, and
> that he was overstating his case, as has been explained more than once?


read what I said : all IO INPUT functions
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      08-04-2008
wrote:

> On 4 août, 21:59, santosh <santosh....@gmail.com> wrote:
>> nicolas.sit...@gmail.com wrote:
>> > On 4 août, 21:30, Eric Sosman <Eric.Sos...@sun.com> wrote:
>> >> nicolas.sit...@gmail.com wrote:
>> >> > OK OK, everybody seems to be ok to say that all I/O functions
>> >> > can potentially set/clear the end-of-file indicator, but nobody
>> >> > give me normative reference, and the author says that nothing in
>> >> > the C99 says that all I/O functions can set/clear end-of-file
>> >> > indicator!!!

>>
>> >> The normative reference is section 7.19 of the Standard.
>> > I looked at this section, but I didn't find anything saying clearly
>> > that all IO INPUT functions set/clear the end-of-file indicator!

>>
>> Hasn't it been made clear to you by now that only one poster has said
>> that all functions that perform an I/O operation set these flags, and
>> that he was overstating his case, as has been explained more than
>> once?

>
> read what I said : all IO INPUT functions


You have already been told that all input in C is done as if it is
composed of one or more calls to fgetc, and fgetc can most certainly
set the end-of-file indicator, and it's implied that other input
functions modelled on top of it, must also do so. Indeed they have no
choice but to do so. However they do have different methods of
notifying the user of such an exception. For example fgets returns a
null pointer, fread returns a short item count, scanf returns EOF etc.
In all cases you can always determine whether a particular FILE stream
is at end-of-file by calling the feof function, and whether an I/O
error has occurred during the last access by using the ferror function.

 
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
Code to automatically write C get/set functions from a set of typedef structs? David Mathog C Programming 9 05-26-2012 08:09 PM
Re: functions which take functions Antti J Ylikoski Python 1 04-13-2012 01:23 PM
Microcontrollers: which one ? which language ? which compiler ? The Jesus of Suburbia NZ Computing 2 02-11-2006 06:53 PM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM
Which implementation of "set/get" member functions is better? Please help. Me C++ 12 08-18-2003 07:13 PM



Advertisments