Welcome to easyCPlusPlus.com's tutorial on C programming. This lesson covers reading and
writing to files and passing command line arguments into your code. An important part
of any program is the ability to communicate with the world external to it. Reading
input from files and writing results to files are simple, but effective ways to
achieve that. Command line arguments provide a way to pass a set of arguments into a
program at run time. These arguments could be the names of files on which to operate,
user options to modify program behavior, or data for the program to process.
Library Functions for File I/O.
This section introduces the library
for file input and output (I/O).
These routines are used to open and close files and read and write files using formatted
I/O. Formatted I/O was introduced in lesson 4.
You may wish to review that material.
Fopen is used to open a file for formatted I/O and to associate a stream with that file. A stream
is a source or destination of data. It may be a buffer in memory, a file or some hardware device
such as a port. The prototype for fopen is:
FILE *fopen(const char *filename, const char *mode);
Fopen returns a file pointer on success or NULL on failure. The file pointer is
used to identify the stream and is passed as an argument to the routines that read, write
or manipulate the file. The filename and mode arguments are standard null-terminated
strings. The valid modes are shown below.
||open for reading
||open or create for writing. Truncate (discard) any previous contents.
||open or create for writing. Append (write after) any previous contents.
||open file for update (reading and writing).
||open or create file for update. Truncate (discard) any previous data.
||open or create file for update. Append (write after) any previous data.
Fflush is used to flush any buffered data out of an output stream. Output streams may be
buffered or unbuffered. With buffered output, as data is written to the stream, the
operating system saves this data to an intermediate buffer. When this buffer is full, the data
is then written to the file. This is done to reduce the number of system calls needed to
write out the data. The whole buffer is written at once. This is done to make the program more
efficient and without any programmer involvement. Fflush forces the buffer to be written out
to the associated file. Its prototype is:
int fflush(FILE *stream);
It accepts a file pointer as an argument. It returns zero on success and EOF on failure.
EOF is a special constant used to designate the end of a file.
Files are closed with the function fclose. Its prototype is:
int fclose(FILE *stream);
Fclose returns zero on success and EOF on failure.
Data is written to a file using fprintf. This function is very similar to printf, which
is described fully in lesson 4
. Printf was used to write to standard
output, stdout. Fprintf has one additional argument to
specify the stream to send data. Its prototype is:
int fprintf(FILE *stream, const char* format, ....);
Fprintf returns the number of characters written if successful or a negative number
Data is read from a file using fscanf. This function is very similar to scanf, which was described
in lesson 4 and is used to read from standard input, stdin. Fscanf has one additional
argument to specify the stream to read from. Remember that the argument to store data must be
pointers. The prototype for fscanf is:
int fscanf(FILE *stream, const char* format, ....);