Skip to content
Hub Nexus

No author yetClaim it

Version 1Updated 3 min read

Support

File descriptors

While not specified by the C standard, all modern operating systems use file descriptors (sometimes abbreviated as fd) under-the-hood to identify open files. The FILE type from stdio.h and its associated functions encapsulates the low-level details of a stream, which includes a file descriptor the operating system is keeping track of.

This section explores file descriptors in POSIX systems, like Linux.

Standard streams as file descriptors

During process creation, the operating system allocates (among other resources) three standard streams: stdin, stdout, and stderr. Typically, their FILE-based definitions in stdio.h, as covered in an earlier section, are used to interact with them. These streams can also be interacted with through their raw file descriptors:

unistd.h symbolstreamFile descriptor
STDIN_FILENOstdin0
STDOUT_FILENOstdout1
STDERR_FILENOstderr2

These file descriptors are the same for every process, even though the standard streams contain different data for each process. File descriptors are not unique system-wide; each process has a different view of which file descriptors map to which streams, just like how each process has a different view of the system's virtual address space.

Basic reading and writing

Reading to and writing from a file descriptor can be done with these functions:[1]

#include

ssize_t read(int fd, void *buf, size_t count);
ssize_t write(int fd, const void *buf, size_t count);

Compare them with the FILE-based functions:[2]

#include

char *fgets(char *s, int size, FILE *stream);
int fputs(const char *s, FILE *stream);

Three differences are apparent:

  • Raw file descriptors are used instead of FILEs.
  • The data being read from or written to the stream aren't assumed to be strings.
  • The return values' types are consistent.

read and fgets have similar parameters: something representing the stream, a buffer, and a size. Also, if the amount of data read equals the requested size, the buffer will have the same contents, regardless of the function used. However, these functions behave differently when less data is read:

  • fgets, meant for strings, stops reading early if a newline is encountered, and the function may block if it is waiting for the rest of the string to appear in the stream.
  • read doesn't block; it stops reading early if not all the requested data is in the stream yet.

This makes read more appropriate for situations where the programmer needs more control over the type of the data being read or is willing to trade receiving partially-read data for reducing the number of blocking I/O operations.

write needs an explicit size parameter since it can't assume a NULL-terminated string (or a string at all) is being written, and it will return the number of bytes written so the program can determine whether the passed data was fully written to the stream.

FILE-file descriptor conversions

Turn a stream into a file descriptor using:

#include

int fileno(FILE *stream);

And turn a file descriptor into a stream like so:

FILE *fdopen(int fd, const char *mode);

Like most stream-based functions, it will return NULL on failure. See C programming/Stream IO#Opening Files for mode values.

Port I/O

Port I/O functions live in sys/io.h and have the following naming scheme:

  • Direction of I/O
    in to read
  • out to write
  • Data type

b for unsigned char

  • w for unsigned short
  • l for unsigned int

Here are example function signatures for reading and writing:

#include

unsigned char inb(unsigned short port);
void outw(unsigned short value, unsigned short port);

References

  • ↑ read(2) and write(2), Linux Programmer's Manual, 2019-10-10
  • ↑ fgets(3) and fputs(3), Linux Programmer's Manual, 2020-08-13

Where this page came from

This page was imported from Wikibooks. From “C programming” on Wikibooks, by its contributors, under CC BY-SA 4.0. Changed here: set as a page; navigation and edit links left out; each image under its own licence, credited in its caption.

Nobody has written it yet — it is the source material at a new address, which is why search engines are asked to skip it and why no one earns from it. It is up for grabs: take it on, and it is yours to rewrite and to earn from.

Get each new page of

C programming

Following means one email when a page is added, nothing when a page changes.

Following means one email when a page is added, nothing when a page changes.

Comments

Spinner Logo

Next in C programming

CC BY-SA 4.0
We can create the necessary subdirectory structure by running "h2xs" application (e.g. "h2xs -A -n Modulename"). This will create - among others - a Makefile.PL, a .pm Perl module and a .xs XSUB file in the subdirectory tree. We can edit the .xs file by adding our code to that, let's say: void hello
CC BY-SA 4.0
Since the C Programming-Language was not created with Object Oriented Programming in mind, it has no explicit support for classes, inheritance, polymorphism and other OO Concepts. Neither does it have its own Virtual Table, which is found in object-oriented languages such [C++]( [Java]( and [C#]( Th
CC BY-SA 4.0
A **statement** is a command given to the computer that instructs the computer to take a specific action, such as display to the screen, or collect input. A computer program is made up of a series of statements. In C, a statement can be any of the following: ## Labeled statements A statement can be
CC BY-SA 4.0
GCC, Clang) to emit a diagnostic message: #define EMIT_MESSAGE(str) EMIT_PRAGMA(message(str)) #define EMIT_PRAGMA(content) _Pragma(#content) EMIT_MESSAGE("Hello, world!") Some compilers use a slight variant syntax; in particular, MSVC supports **__pragma** instead of **_Pragma**. Specific compilers
CC BY-SA 4.0
The **C POSIX library** is a language-independent library (using C calling conventions) that adds functions specific to POSIX systems. POSIX (and the Single Unix Specification) specifies a number of routines that should be available over and above those in the C standard library proper. It was devel
CC BY-SA 4.0
C was designed in English and assumes the common [ASCII]( character set made for English, which includes such characters as {, }, [, ], and so on. Some other character sets (like [EBCDIC]( on mainframes), however, do not have these or other characters which are required by C. Since even the simplest