Lab Assignment--System Calls for I/0


Exercise Description

  1. Create a file called openClose.in. The contents of the file can be this.
  2. Write a C++ program 'openClose.cpp' that copies openClose.in to a file called openClose.out. Use open(), read(), write() and close() system calls. Use a buffer size of 32 bytes for reads and writes.

    A call to your program will look something like this:

    openClose openClose.in openClose.out

    If you have the wrong number of arguments, your code will produce a message like the following:

    %openClose
    Usage: openClose <inFile> <outFile>

    Check for errors (when opening the files) and use perror. For instance, you should see something like the following if the user types an nonexistent input file:

    %openClose noSuchFile openClose.out
    
    inFile is noSuchFile fd = -1         errno = 2
    InFile Error: No such file or directory
            
  3. Call fstat() in the openClose.cpp program and print the following information about openClose.out: the file size (in bytes), the owner and group IDs, and the time it was last modified. Hint: do not perform a close on the file decriptor that goes with the openClose.out file until after fstat(). The output can be in the following format:
    This is the status of openClose.out
    File Size      Owner       Group ID    Last Modified
    815            10064       200         1097124530
    If you want to do more, you can output in the following format:
    This is the status of openClose.out
    File Size      Owner       Group ID    Last Modified
    815            nova        csfac       Wed Oct  6 22:48:50 2004
    
    To do this, you can make use of library calls: getgrgid, getpwuid, and ctime. Check out the man pages for these calls.
  4. You do not have to use the write system call for this last step. If you want, you can use printf to format your output nicely. For more on printf(), click here.

Deliverables

Submit 2 files to URCourses

  1. openClose.cpp
  2. Script containing the following commands:
    1. ls -l (Goal: show that openClose.out does not exist)
    2. cat openClose.in (Goal: display the contents of the input file)
    3. openClose (Result: error message-not enough arguments)
    4. openClose openClose.in (Result: error message-not enough arguments)
    5. openClose noSuchFile openClose.out (Result: error message-no such file)
    6. openClose openClose.in openClose.out (Result: no errors; runs to completion and creates openClose.out)
    7. ls -l (Goal: show that openClose.out has been created)
    8. cat openClose.out (Goal: display the contents of the output file)
    9. openClose openClose.in  /openClose.out (Result: error message-permission denied on creating openClose.out)

Notes