Solved

The _IO_FILE typedef

Posted on 1998-05-21
4
2,106 Views
Last Modified: 2008-02-01
In Kernighan and Ritchie's book on C, they say that the definition of FILE is to be found in <stdio.h>; they say it is defined to be an _iobuf struct. On my Linux system, though, FILE is defined differently: in <stdio.h> it's defined as an _IO_FILE struct. _IO_FILE in turn is defined as a structure in <libio.h>. My question is: there seems to be nothing corresponding here to the fd member of the _iobuf struct that Kernighan and Ritchie talk about. So how (for example) would fclose on my system be related to the close system call?
0
Comment
Question by:vexil
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 

Expert Comment

by:minar
ID: 1637198
In general, it's better to assume that a FILE * pointer is
opaque: don't try to read inside it. Code that mucks inside
FILE * is hard to make portable.

If you need to get the file descriptor for a FILE * pointer,
use the fileno() macro. So for instance, fclose(FILE * foo)
is comparable to close(fileno(FILE * foo)). (But don't mix
the two styles of file I/O unless you know what you're doing).
0
 

Author Comment

by:vexil
ID: 1637199
Thanks for answering, Minar, but I'm interested in the question in a theoretical rather than a practical sense. But I'll give you some points if you tell me where the fileno macro is.
0
 

Accepted Solution

by:
minar earned 200 total points
ID: 1637200
Ah, you want theory! In practice, again, fileno() is the right way to get a file descriptor from a FILE *. But if you want to get into the guts, take a look at the source to libc.

In Linux libc5, the _IO_FILE struct has an integer member _fileno; This is the actual file descriptor; it's set up in libc's libio/fileops.c, the function _IO_file_fopen. The stdio library does some buffering but basically uses the underlying file descriptor for IO.

fileno() itself is a function in libio/stdio/fileno.c. It does some checks for EOF and then passes through to the _IO_fileno macro in libio/iolibio.h. That just returns (FP)->_fileno.

GNU libc6 will probably be different.

0
 

Author Comment

by:vexil
ID: 1637201
Thanks!
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
In my business, I use the LTS (Long Term Support) versions of Linux. My workstations do real work, and so I rarely have the patience to deal with silly problems caused by an upgraded kernel that had experimental software on it to begin with from a r…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question