POSIX Files API

POSIX functions for working with files include

Standard file descriptors open by default

  • 0 for stdin
  • 1 for stdout
  • 2 for stderr

stdio library in C works on top of these functions

Manipulating File Descriptors

dup2

int dup2(int oldfd, int newfd);
Example

fd = open(infile, O_RDONLY);
dup2(fd,0);

the infile is opened and is set to stdin

  • reading from stdin now reads from infile

fcntl (file control)

int fcntl(int fd, F_SETFD, int val);

Pipes

int pipe(int fds[2]);