Section 3 from Reading and writing with files
Write a program with name
fwrite_getline.c
(which you will find in your
folder fwrite
) that implements the
following operations:
Check the program is started with a single argument. If that is not the case, print a message and terminate.
Open the file using as name the argument given to the program. If the operation fails, notify with a message and terminate the execution.
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
.
Close the file and if the operation produces an error, notify it with a message on the screen.
Upload the program to the folder
fwrite
in your workspace in
Subversion.