Objectives
We would like to :
- Understand the basics of File I/O
Notes
- This is chapter 4, you should read it.
- This is the first of several chapters that cover I/O
- in the end, Figure 5-2 page 95 is important
- It will take a while to understand
- But once you do, it will be great
-
- Quick review of files in c++
- File Descriptors
- A file descriptor is a non-negative integer.
- As you can see above, it is just an index into a table associated with your process.
- The cool part of UNIX/Linux is that everything is a file
- A file descriptor can point to
- A regular file
- Including hard links
- And soft links
- A pipe
- A socket
- Devices (/dev/pts/0) (block and character)
- Missing from this list are directories
- They have their own special methods.
- Most programs start with three open file descriptors
- 0: standard input, (cin in c++) , STDIN_FILENO
- 1: standard output, (cout in c++), STDOUT_FILENO
- 2: standard error, (cerr in c++) , STDERR_FILENO
- The file names are from unistd.h
- These are inherited from the parent process.
- There are four main file manipulation routines
-
open
takes a path and some flags and returns a file descriptor
-
close
takes a file descriptor and returns the status
-
read
takes a file descriptor, array and size and returns the number of bytes read.
-
write
takes a file descriptor, array and size and returns the number of bytes written.
- There are some additional routines as ell
-
-
creat
a form of open for creating new files.
-
lseek
allows us to move around in a file.
-
ioctl
Additional file manipulation.
- Note, all of these operations are on the byte level, not character!
- I/O is refereed to as Universal as read/write/open/close work on most files.
- Take a look at demo1.cpp
- od -x binaryFile
- hexdump binaryFile