UC3M

Telematic/Audiovisual Syst./Communication Syst. Engineering

Systems Architecture

September 2017 - January 2018

9.8.3.  Storing strings in a file (with fwrite)

Resources

Work Plan

  1. Write a program with name fwrite_getline.c (which you will find in your folder fwrite) that implements the following operations:

    1. Check the program is started 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. Asks the user for a string or CTRL+D to finish the program (it will use the getline function).

      Calculate the actual size of the string (with strlen) and store its size and the string itself, using the fwrite function. When users desire to finish the program, they must enter CTRL+D.

      The program checks the CTRL-D condition, looking the return value of getline (read about getline in its manual page).

      The function getline executes internally a malloc (if the user does not do it explicitly); therefore, the number of calls to free must be identical to the calls to malloc.

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

  2. Upload the program to the folder fwrite in your workspace in Subversion.