2. Basic File I/O

2.1 Introduction to File I/O

In this chapter we’ll explore basic I/O on regular files. The I/O story continues in Chapter 3 with more advanced I/O system calls. I/O on special files is in Chapter 4, I/O on pipes in Chapter 6, I/O on named pipes in Chapter 7, and I/O on sockets in Chapter 8.

To get started I’ll show a simple example that uses four system calls you may already be familiar with: open, read, write, and close. This function copies one file to another (like the cp command):

image

Try to find the bug in this function (there’s a clue in Section 1.4.1). If you can’t, I’ll point it out in Section 2.9.

I’ll say just a few quick ...

Get Advanced UNIX Programming, Second Edition 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.