Read/Write
Objectives
We would like to :
- Understand the read and write system calls.
Notes
-
ssize_t read(int fd, void * buffer, size_t count);
-
ssize_t write(int fd, const void * buffer, size_t count);
- Both:
- require unistd.h
- Take the correct type of open file descriptor.
- Take a buffer, the data to read/write
- Take a size, the number bytes to read/write
- Return the actual number of bytes read/written
- This may be less than the requested amount
- Lack of data to read
- Lack of space/resource to write.
- Return -1 on error
- THESE FUNCTIONS DEAL WITH RAW I/O
- Whatever BYTES are in the buffer/file will be transferred
- This is opposed to << , >> or getline in c++ where the bytes will be converted to ascii then written.
-
int close(int fd);
- Close the file.
- If the file was removed in any way, when the last open file descriptor is closed, the file is destroyed.
- returns
- 0 on success
- -1 on error.