JMU
UNIX Input/Output
Universal File I/O


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


Review
Universal Operations
Opening a File: open() open
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

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

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
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

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

Reading from a File: read() read
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.)
Reading from a File: Some Values of errno

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
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

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
unixexamples/io/error_handler.h
        #ifndef ERROR_HANDLER_H
#define ERROR_HANDLER_H

void
handle_error();

#endif
        
unixexamples/io/error_handler.c
        #include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include "error_handler.h"


void
handle_error()
{
  perror("Error");
  exit(EXIT_FAILURE);
}
        
An Example: The Application
unixexamples/io/coursewriter.c
        #include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include "error_handler.h"

#define  LENGTH 5

int
main(int argc, char **argv)
{
  char       level;
  char       id[LENGTH];
  int        fd;
  ssize_t    n;

  level = '0';
  fd = open("courses.txt", O_RDONLY);
  if (fd <0) handle_error();

  
  while ((n = read(fd, id, LENGTH)) == LENGTH)
    {
      if(id[2] != level)
        {
          level = id[2];
          write(STDOUT_FILENO, "\n", 1);
        }
      write(STDOUT_FILENO, id, LENGTH);
      write(STDOUT_FILENO, "\n", 1);
    }

  close(fd);
}