System programming in OCaml – Part 2

Now we are going to take a look at manipulating files in OCaml. We will be focusing on the Unix file system.

In Unix the term “file” refers to a greater range of objects that you might expect. This includes:

  • standard files like .txt, .html, .doc or .ml
  • directories (or folders if your used to windows) like /photos or /music
  • symbolic links (or shortcuts if your used to windows)
  • special files to access devices i.e. files in /dev
  • named pipe ( this is a named version of using | in the terminal )
  • sockets

All files are represented by an i-node, a data structure which holds the meta-data on the file. Directories are files that map filenames to i-nodes. The directories form a tree structure, where all files are directly or indirectly children of the root directory called ‘/’.

A absolute path name of the file is one that begins at the root. A relative path name is one that begins in the current directory. Every directory has ‘.’ and ‘..’ which are symbolic links to the current and parent directories.

OCaml makes use of the Filename module to help handle paths in a portable manner

Leave a Reply