- read, write, close
- This is not as complex
- close
- int close(int fd);
- Can fail, but what do you do if it does?
- man 2 close warns that it is a serious programming error not to check the results of a close.
- Frees the file descriptor.
- There are problems that can occur on NFS mounted file systems.
- If this is the last reference to the file, it will be removed.
- Close does not sync the file, it may be buffered,
- use int fsync(int fd); to sync the buffer.
- Or non-buffered files, we will discuss later.
- read/write
- ssize_t read(int fd, void *buf, size_t count);
- ssize_t write(int fd, const void *buf, size_t count);
- Perform raw i/o
- Given a buffer, attempt to read/write that many bytes
- Returns
- -1 on error
- the number of bytes read or written.
- There is the possibility of a partial read/write problem
- lseek: Move around in a file
- off_t lseek(int fd, off_t offset, int whence);
- fd is a valid file descriptor
- offset is the size in bytes to move.
- whence where to move
- SEEK_SET : set the file offset to a position
- Negative is not acceptable
- You can seek past the end of a file, this is ok.
- SEEK_CUR: the current location + the offset
- This can include a negative offset
- But you can not seek before the start of a file.
- SEEK_END
- End of the file plus offset
- This is one past the end if offset = 0.
- The return value is the position (in bytes) in the file.
- Writing past the end of the file can create a hole.
- This is not a bug but a feature.
- You can write into holes.
- If you read data from a hole, the value is 0.
- look at hole.C
- look at
- Look at seeker.C in some detail