JMU
UNIX Input/Output
Working with Disk Files and the File System


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


Review
Some Common (though Not Universal) Operations
Changing the File Offset: lseek() lseek
off_t lseek(int fd, off_t offset, int whence);
Purpose:
Reposition the (logical) file offset of an open file descriptor (without causing any physical device access)
Details:
fd The file descriptor
offset The offset (in bytes)
whence The base point from which offset is to be interpreted
Return The new file offset; -1 on error
#include <unistd.h> unistd.h
Changing the File Offset: whence Values

SEEK_SET The base is the beginning of the file (i.e., the file offset is set to offsetn which must be non-negative)
SEEK_CUR The base is the current offset
SEEK_END The base is the size of the file

Some Common Idioms
  // Obtain the current file offset
  current = lseek(fd, 0, SEEK_CUR);

  // Set the file offset to the start of the file
  lseek(fd, 0, SEEK_SET);

  // Set the file offset to the next byte after the end of the file
  lseek(fd, 0, SEEK_END);
  
A Common Sequence of Calls
  // Save the current file offset
  original = lseek(fd, 0, SEEK_CUR);

  // Set the file offset to a particular value
  lseek(fd, offset, SEEK_SET);

  // Read from the new file offset
  size = read(fd, buffer, length);

  // Reset the file offset
  lseek(fd, original, SEEK_SET);
  

Unfortunately, this sequence of calls is not atmoic (i.e., might be interrupted)!

Reading from an Offset: pread() pread
ssize_t pread(int fd, void* buffer, size_t count, off_t offset);
Purpose:
Atomically read from a specific file offset, without changing the offset
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
offset The file offset to copy from
Return The number of bytes copied; 0 on EOF; -1 on error
#include <unistd.h> unistd.h
Writing to an Offset: pwrite() pwrite
ssize_t pwrite(int fd, const void *buffer, size_t count, off_t offset
Purpose:
Atomically write to a specific file offset, without changing the offset
Details:
fd The file descriptor to copy to
buffer The memory to copy from
count The maximum number of bytes to copy
offset The file offset to copy to
Return The number of bytes copied; -1 on error
#include <unistd.h> unistd.h