UC3M

Telematic/Audiovisual Syst./Communication Syst. Engineering

Systems Architecture

September 2017 - January 2018

9.8.12.  Open and close a file

Resources

  • Section 1 and 2 from Reading and writing with files

  • Copy the template CFile.templ from the directory Templates of your shared folder to the subfolder fopen_fclose.

Work Plan

Write a program with name main.c that implements the following operations (start from the template in the file CFile.templ which can be found in the subfolder fopen_fclose of your shared folder):

  1. Any C program can be executed from the command line adding words separated by spaces. The main function receives as parameters an integer (typically it is given the name argc) with the number of arguments written when invoking the program (the name of the program counts), and as second parameter an array of pointers to the strings written in the command line (thus, of type char **).

    Write code in the main to check that the program is executing with a single argument. If that is not the case, print a message and terminate.

  2. Open the file using as name the argument given to the program. If the operation fails, notify with a message and terminate the execution.

  3. Close the file and if the operation produces an error, notify it with a message on the screen.

Check the program with different file names, and also with names that do not correspond to any file.

Upload the program to the folder fopen_fclose in your workspace in Subversion.