The FILE Structure
Where system calls such as open() and dup() return a file descriptor through which the file can be accessed, the stdio library operates on a FILE structure, or file stream as it is often called. This is basically a character buffer that holds enough information to record the current read and write file pointers and some other ancillary information. On Linux, the IO_FILE structure from which the FILE structure is defined is shown below. Note that not all of the structure is shown here.
struct _IO_FILE { char *_IO_read_ptr; /* Current read pointer */ char *_IO_read_end; /* End of get area. */ char *_IO_read_base; /* Start of putback and get area. */ char *_IO_write_base; /* Start of put area. */ char *_IO_write_ptr; /* Current put pointer. */ char *_IO_write_end; /* End of put area. */ char *_IO_buf_base; /* Start of reserve area. */ char *_IO_buf_end; /* End of reserve area. */ int _fileno; int _blksize; }; typedef struct _IO_FILE FILE;
Each of the structure fields will be analyzed in more detail throughout the chapter. However, first consider a call to the open() and read() system calls:
fd = open(“/etc/passwd”, O_RDONLY); read(fd, buf, 1024);
When accessing a file through the stdio library routines, a FILE structure will be allocated and associated with the file descriptor fd, and all I/O will operate through a single buffer. For the _IO_FILE structure shown above, _fileno is used to store the file descriptor that is used on subsequent calls to read() or ...
Get UNIX Filesystems: Evolution, Design, and Implementation now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.