Lecture 6: File I/O

Introduction

In Unix, Everything is a File…

Files & Directories

                   foo/
                    |
          +-----+---+---+--------+
          |     |       |        |
         bar/  baz/  info.txt hello.c        
          |     |
      img.png   |
                |
       +--------+-------+-------+-------+
       |        |       |       |       |
    meme.gif run.sh items.csv main.s letter.docx

File System API

File Descriptors

Default Descriptors

Descriptor Purpose
0 standard input (stdin)
1 standard output (stdout)
2 standard error output (stderr)

Opening and Closing Files

Reading from Files

Writing to a File

(Use strace to trace cat foo)

Other operations

Fun With Redirection