Solved

The _IO_FILE typedef

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
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…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

710 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