- Forward


UNIX Input/Output
Universal File I/O


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Review
Back SMYC Forward
  • Files in UNIX:
    • A file is a sequence of bytes
    • All I/O devices are modeled as files
    • A device driver is the code that allows for (the standard set of) operations to be performed on an I/O device
  • File Descriptors:
    • A (usually small) nonnegative integer that an application can use to identify a file
Universal Operations
Back SMYC Forward
  • Open:
    • State the intent to use a file
  • Read:
    • Copy \(n > 0\) bytes from a file (at the current position) to memory and update the current position (possibly triggering an end-of-file condition)
  • Write:
    • Copy \(n > 0\) bytes from memory to a file (at the current position) and update the current position
  • Close:
    • State that the device will no longer be used
Opening a File: open() open
Back SMYC Forward
int open(char *pathname, int flags, mode_t mode);
Purpose:
Open (and possibly create) a file.
Details:
pathname The path to the file
flags A bit mask specifying how the file will be accessed
mode An optional (except when creating) access permission
Return A file descriptor (or -1 if an error occurs)
#include <sys/types.h> sys/types.h
#include <sys/stat.h> sys/stat.h
#include <fcntl.h> fcntl.h
Opening a File: Common flags
Back SMYC Forward

O_RDONLY Read only
O_WRONLY Write only
O_RDWR Read and write
O_CREAT Create the file if it doesn't exist
O_APPEND Writes are always appended to the end of the file

Opening a File: Some Values of errno
Back SMYC Forward

EACCES The file permissions don't allow the mode specified by flags
EISDIR Attempted to open a directory for writing
EMFILE Too many open files (for the process)
ENFILE Too many open files (for the system)

Closing a File: close() close
Back SMYC Forward
int close(int fd);
Purpose:
Dissociate a file descriptor from its file and make it available for reuse
Details:
fd The (open) file descriptor to close
Return 0 on success; -1 on error
#include <unistd.h> unistd.h
Closing a File: Some Values of errno
Back SMYC Forward

EBADF fd isn't a valid open file descriptor
EIO An I/O error occurred

Reading from a File: read() read
Back SMYC Forward
ssize_t read(int fd, void *buffer, size_t count)
Purpose:
(Attempt to) copy a sequence of bytes from a file into memory
Details:
fd The file descriptor to read from
buffer The ultimate location of the bytes in memory
count The maximum number of bytes to copy
Return The number of bytes copied; 0 on EOF; -1 on error
#include <unistd.h> unistd.h
Reading from a File (cont.)
Back SMYC Forward
  • The Buffer:
    • Must be at least count bytes long
    • Must have been allocated before the call
  • The Number of Bytes Read:
    • May be less than the requested number (e.g., if EOF is encountered, if the file is line-oriented and an LF is encounted)
  • What is Read:
    • A sequence of bytes (NOT a C string so it will not be null-terminated)
Reading from a File: Some Values of errno
Back SMYC Forward

EBADF fd is invalid or not open for reading
EIO I/O error
EISDIR fd refers to a directory

Writing to a File: write() write
Back SMYC Forward
ssize_t write(int fd, const void *buffer, size_t count);
Purpose:
Copy bytes from memory to a file
Details:
fd The file descriptor to copy to
buffer The memory to copy from
count The maximum number of bytes to copy
Return The number of bytes copied; -1 on error
#include <unistd.h> unistd.h
Writing to a File: Some Values of errno
Back SMYC Forward

EBADF fd is invalid or not open for reading
EDQUOT The user's disk quota has been exhausted
EIO I/O error
ENOSPC The device has no room for the data

An Example: The Error Handler
Back SMYC Forward
unixexamples/io/error_handler.h
 
unixexamples/io/error_handler.c
 
An Example: The Application
Back SMYC Forward
unixexamples/io/coursewriter.c
 
There's Always More to Learn
Back -