Monday, August 27, 2007

File Input/Output :: Other Functions

  • flock - Provides a file lock on the filehandle.
  • seek - Provides the ability to move the file pointer within the file.
  • Example of the above functions:
      open(outf, ">>log.out");
    flock(outf, 2);
    seek(outf, 0, 2);
    # ... some code
    close(outf);
    We are opening a log.out file for appending, and immediately lock the file with 2, which is LOCK_EX for an exclusive lock. This is typically used for writing. You can flock files for reading using 0 for LOCK_SH or a shared lock. The seek seeks to the end of the file if there was writing done to the log.out before the flock was called. seek takes in the filehandle, offset, and whence arguments. 0 is the beginning of the file, 1 is the current position in the file, 2 is for the end of the file. Note that closing any filehandles automatically frees all file locks.

File Input/Output :: Error Handling

No comments: