Solved

The _IO_FILE typedef

Posted on 1998-05-21
4
1,984 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
This article will explain how to establish a SSH connection to Ubuntu through the firewall and using a different port other then 22. I have set up a Ubuntu virtual machine in Virtualbox and I am running a Windows 7 workstation. From the Ubuntu vi…
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.

743 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

12 Experts available now in Live!

Get 1:1 Help Now