alp-apB-low-level-io - 16 0430 APPB 10:58 AM Page 281 B...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Low-Level I/O B C PROGRAMMERS ON GNU/L INUX HAVE TWO SETS OF INPUT / OUTPUT functions at their disposal.The standard C library provides I/O functions: printf , fopen , and so on. 1 The Linux kernel itself provides another set of I/O operations that operate at a lower level than the C library functions. Because this book is for people who already know the C language, we’ll assume that you have encountered and know how to use the C library I/O functions. Often there are good reasons to use Linux’s low-level I/O functions. Many of these are kernel system calls 2 and provide the most direct access to underlying system capa- bilities that is available to application programs. In fact, the standard C library I/O routines are implemented on top of the Linux low-level I/O system calls. Using the latter is usually the most efficient way to perform input and output operations—and is sometimes more convenient, too. 1.The C++ standard library provides iostreams with similar functionality.The standard C library is also available in the C++ language. 2. See Chapter 8,“Linux System Calls,” for an explanation of the difference between a system call and an ordinary function call.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
282 Appendix B Low-Level I/O Throughout this book, we assume that you’re familiar with the calls described in this appendix.You may already be familiar with them because they’re nearly the same as those provided on other UNIX and UNIX-like operating systems (and on the Win32 platform as well). If you’re not familiar with them, however, read on; you’ll find the rest of the book much easier to understand if you familiarize yourself with this material first. B.1 Reading and Writing Data The first I/O function you likely encountered when you first learned the C language was printf .This formats a text string and then prints it to standard output.The gener- alized version, fprintf , can print the text to a stream other than standard output.A stream is represented by a FILE* pointer.You obtain a FILE* pointer by opening a file with fopen. When you’re done, you can close it with fclose . In addition to fprintf , you can use such functions as fputc , fputs , and fwrite to write data to the stream, or fscanf , fgetc , fgets , and fread to read data. With the Linux low-level I/O operations, you use a handle called a file descriptor instead of a FILE* pointer.A file descriptor is an integer value that refers to a particu- lar instance of an open file in a single process. It can be open for reading, for writing, or for both reading and writing.A file descriptor doesn’t have to refer to an open file; it can represent a connection with another system component that is capable of send- ing or receiving data. For example, a connection to a hardware device is represented by a file descriptor (see Chapter 6,“Devices”), as is an open socket (see Chapter 5, “Interprocess Communication,” Section 5.5,“Sockets”) or one end of a pipe (see Section 5.4,“Pipes”). Include the header files
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 20

alp-apB-low-level-io - 16 0430 APPB 10:58 AM Page 281 B...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online