Solved

The _IO_FILE typedef

Posted on 1998-05-21
4
2,060 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
  • 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
The purpose of this article is to show how we can create Linux Mint virtual machine using Oracle Virtual Box. To install Linux Mint we have to download the ISO file from its website i.e. http://www.linuxmint.com. Once you open the link you will see …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

813 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now