Solved

The _IO_FILE typedef

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Fine Tune your automatic Updates for Ubuntu / Debian
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…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

910 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

16 Experts available now in Live!

Get 1:1 Help Now