Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #9804

Re: Function to determine the number of chars in a FILE buffer

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: Function to determine the number of chars in a FILE buffer
Date 2011-09-01 06:58 -0400
Organization A noiseless patient Spider
Message-ID <j3nohj$i6f$1@dont-email.me> (permalink)
References <5303ab8c-2dca-4c04-bb88-49e26fc92f96@n11g2000yqh.googlegroups.com> <4E5EBC04.3060103@verizon.net> <slrnj5u9a8.35d.angel+news@pearlgates.net> <7aa0a948-3af6-40b2-8129-4d14d3f83ecb@gz5g2000vbb.googlegroups.com>

Show all headers | View raw


On 09/01/2011 03:23 AM, tm wrote:
> On 1 Sep., 08:25, Angel <angel+n...@spamcop.net> wrote:
>> On 2011-08-31, James Kuyper <jameskuy...@verizon.net> wrote:
>>
>>> On 08/31/2011 06:43 PM, tm wrote:
>>
>>> A fully portable C program cannot call popen() and has no way to be sure
>>> whether or not a stream that it opens is a FIFO. You'll get better
>>> answers to your questions in a newsgroup associated with the library
>>> that provides popen() (and select() and fileno(), and all of the other
>>> implementation-specific features referred to in your program).
>>
>> Those are POSIX functions, I would guess he is working on some brand of
>> Unix and would thus be better off in comp.unix.programmer.
> 
> I am writing code for Unix and Windows, but my question
> is about a FILE *, which is standard C.

And the standard C answer is, it can't be done. The C standard library
deliberately hides such details from you. There may be Unix- or
Windows-specific solutions, but no portable C solution.

> When you call getc(aFile) two things can happen:
> 
>   1. The character is already in the FILE buffer.
>      In this case the char is returned from the FILE
>      buffer and the read position of the FILE buffer
>      is adjusted.
>   2. The FILE buffer is empty.
>      In this case a block of e.g. 1024 characters
>      is read from the underlying file descriptor
>      into the FILE buffer. Afterwards the first
>      character from the FILE buffer is returned.
> 
> What I need is:
> Find out if the next getc() will use case 1 or case 2.
> 
> Historically getc() was defined as macro.

The fact that getc() is allowed to be a macro is the main purpose of
getc(); it's otherwise redundant with fgetc(). And it could easily be a
macro that expands into a call to fgetc().

> Essentially I need the condition used in the getc() macro.

The problem is that there's no single condition used by that macro for
this purpose. The expansion of that macro is different on different
systems, so the solution to your problem will be different on different
systems.

I've looked at the details of how FILE is defined on various systems.
It's usually a structure containing one pointer to the buffer that it
maintains, and multiple pointers that point at various locations inside
that buffer. The names of those pointers generally make it clear how
they're used, and the quantity you want to calculate is the difference
of two of those pointers - but they'll have different names on different
systems, so you can't portably make use of that fact.

> I could take it from there, but I want to support
> several compilers, libraries and operating systems.
> 
> Thats the reason I ask for a portable solution, to
> find out the number of chars currently in a FILE buffer.

Then you're out of luck.
-- 
James Kuyper

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Function to determine the number of chars in a FILE buffer tm <thomas.mertes@gmx.at> - 2011-08-31 15:43 -0700
  Re: Function to determine the number of chars in a FILE buffer James Kuyper <jameskuyper@verizon.net> - 2011-08-31 18:56 -0400
    Re: Function to determine the number of chars in a FILE buffer Angel <angel+news@spamcop.net> - 2011-09-01 06:25 +0000
      Re: Function to determine the number of chars in a FILE buffer tm <thomas.mertes@gmx.at> - 2011-09-01 00:23 -0700
        Re: Function to determine the number of chars in a FILE buffer Robert Wessel <robertwessel2@yahoo.com> - 2011-09-01 03:27 -0500
        Re: Function to determine the number of chars in a FILE buffer James Kuyper <jameskuyper@verizon.net> - 2011-09-01 06:58 -0400
          Re: Function to determine the number of chars in a FILE buffer Keith Thompson <kst-u@mib.org> - 2011-09-01 08:29 -0700
        Re: Function to determine the number of chars in a FILE buffer Kenneth Brody <kenbrody@spamcop.net> - 2011-09-01 14:37 -0400
        Re: Function to determine the number of chars in a FILE buffer gordonb.nfrza@burditt.org (Gordon Burditt) - 2011-09-02 21:58 -0500
    Re: Function to determine the number of chars in a FILE buffer tm <thomas.mertes@gmx.at> - 2011-08-31 23:54 -0700
      Re: Function to determine the number of chars in a FILE buffer Jasen Betts <jasen@xnet.co.nz> - 2011-09-01 11:02 +0000
        Re: Function to determine the number of chars in a FILE buffer tm <thomas.mertes@gmx.at> - 2011-09-01 05:44 -0700
      Re: Function to determine the number of chars in a FILE buffer David Schwartz <davids@webmaster.com> - 2011-09-01 04:41 -0700
  Re: Function to determine the number of chars in a FILE buffer Ike Naar <ike@iceland.freeshell.org> - 2011-09-01 04:37 +0000
  Re: Function to determine the number of chars in a FILE buffer Mark Storkamp <mstorkamp@yahoo.com> - 2011-09-01 08:23 -0500

csiph-web