POSIX File Operations

Linux is a POSIX compliant Operating System. POSIX stands for Portable Operating System Interface. Basically, this Interface is a standard specified by IEEE for keeping compatibility between operating systems. Therefore, POSIX “defines the application programming interface (API), along with command line shells and utility interfaces, for software compatibility with variants of Unix and other operating systems”(http://en.wikipedia.org/wiki/POSIX).

I would like to give a few POSIX File Operations that are compatible with Linux. These File Operations are used in the C programming language:

  • open()
  • close()
  • read()
  • write()
To use these calls, please recognize, you must include stdio.h, and fcntl.h.  Lets take a look!

open( char*, &char, 1 )

The open() call requires the name and path of the target file.  To read or write to a file, you must first open it.  The call also requires a system pointer address, which will address the first byte of the stream (read or write stream).  You must assign an integer variable to this call, as the integer value is used to identify the open file.


close( int )

This call will close an opened file.  By closing the file, you will release the lock and system resources used for this file.


read( int, &char, 1 )

Thes call requires a file descriptor (which was the integer assigned to open a file), a buffer address (hence the &c), and a buffer length.  The data read will be stored in &char.


write( int, &char, 1 )

This call is very similar to read, except you will be writing data instead of reading.  The data written comes from the &char address.


Here is the piece of C code to utilize these calls:

#include “stdio.h”
#include “fcntl.h”
int main() {
      int inputFile, outputFile;
      char *inputFileName = “readme”;
      char *outputFileName = “writeme”;
      int length;
      char c;
      inputFile = open(inputFileName, O_RDONLY);
      outputFile = open(outputFileName, O_WRONLY);
      /* Loop through the contents of the input file */
      while((length = read(inputFile, &c, 1)) > 0)
         write(outputFile, &c, 1);
      /* Close files before quiting */
      close(inputFile);
      close(outputFile);
}

Now I shall review each line of this code:

  • #include “stdio.h” – This includes into your program the STanDard Input Output Header file. Used for basic communications with keyboard and monitor.
  • #include “fcntl.h” – This header file is the File CoNTroL header. This provides our O_RDONLY and O_WRONLY definitions.
  • int main() – This opens the main body of the program
  • int inputFile, outputFile; – This establishs the inputFile and outputFile variables as integers. These will be used as our File Descriptors.
  • char *inputFileName = “readme”; – This creates a filename and path variable called inputFileName. In this case, the filename is just called readme.
  • char *outputFileName = “writeme”; – This creats a filename and path variable called outputFileName. This describes the file we are going to write to.
  • int length; – This is used to keep track of the length of the file. We do not really do anything with this in this program, other than allow the loop to run through the contents of “readme.”
  • char c; – We will use this as our buffer for reading and writing.
  • inputFile = open(inputFileName, O_RDONLY); – This establishes our File Descriptor (inputFile). The File Descriptor opens the inputFileName(“readme”), and is told to ReaD ONLY.
  • outputFile = open(outputFileName, O_WRONLY); – This establishes another File Descriptor called outputFile, which is told to WRite ONLY to outputFileName(or “writeme”).
  • while((length = read(inputFile, &c, 1)) > 0) – This starts a loop to read in the contents of our input file descriptor into our &c buffer, in the length of 1. This will continue to loop through the contents of file until the end of the file, which is when length = 0, instead of 1 or greater.
  • write(outputFile, &c, 1); – Every iteration of the loop will also write &c buffer to our outputFile file descriptor, at the same length of 1.
  • close(inputFile); close(outputFile); – Then we simply close the files.

Then, just compile this code:

gcc main.c

Create the “readme” file:

touch readme writeme;
echo “important data” > readme;

Then run the program:

./a.out

facebooktwittergoogle_plusredditpinterestlinkedinmailby feather
POSIX File Operations
1 vote, 5.00 avg. rating (99% score)

One thought on “POSIX File Operations

  1. Jack Post author

    Also note, the way that the output file was opened (ie, O_WRONLY), the file *must* exist for the data to copy. If “writeme” doesn’t exist, then this operation will not copy any data to another file.

Leave a Reply