The C standard library I/O functions allow you to read and write data to both files and devices. There are no predefined file structures in C, all data being treated as a sequence of bytes. These I/O functions may be broken into two different categories : stream I/O and low-level I/O.
The stream I/O functions treat a data file as a stream of individual characters. The appropriate stream function can provide buffered, formatted or unformatted input and output of data, ranging from single characters to complicated structures. Buffering streamlines the I/O process by providing temporary storage for data which takes away the burden from the system of writing each item of data directly and instead allows the buffer to fill before causing the data to be written.
The low-level I/O system on the other hand does not perform any buffering or formatting of data --instead it makes direct use of the system's I/O capabilities to transfer usually large blocks of information.
The C I/O system provides a consistent interface to the programmer independent of the actual device being accessed. This interface is termed a stream in C and the actual device is termed a file. A device may be a disk or tape drive, the screen, printer port, etc. but this does not bother the programmer because the stream interface is designed to be largely device independent. All I/O through the keyboard and screen that we have seen so far is in fact done through special standard streams called stdin and stdout for input and output respectively. So in essence the console functions that we have used so far such as printf(), etc. are special case versions of the file functions we will now discuss.
There are two types of streams : text and binary. These streams are basically the same in that all types of data can be transferred through them however there is one important difference between them as we will see.
A text stream is simply a sequence of characters. However the characters in the stream are open to translation or interpretation by the host environment. For example the newline character, '
', will normally be converted into a carriage return/linefeed pair and ^Z will be interpreted as EOF. Thus the number of characters sent may not equal the number of characters received.
A binary stream is a sequence of data comprised of bytes that will not be interfered with so that a one-to-one relationship is maintained between data sent and data received.
Common File Functions:
fopen() open a stream
fclose() close a stream
putc() & fputc() write a character to a stream
getc() & fgetc() read a character from a stream
fprintf() & fscanf formatted I/O
fgets() & fputs() string handling
fseek() position the file pointer at a particular byte
feof() tests if EOF
open a stream
close a stream
write a character to a stream
read a character from a stream
fgets() & fputs()
position the file pointer at a particular byte
tests if EOF
Opening and Closing Files:
A stream is associated with a specific file by performing an open operation. Once a file is opened information may be exchanged between it and your program. Each file that is opened has a unique file control structure of type FILE ( which is defined in along with the prototypes for all I/O functions and constants such as EOF (-1) ). A file pointer is a pointer to this FILE structure which identifies a specific file and defines various things about the file including its name, read/write status, and current position. A file pointer variable is defined as follows
FILE *fptr ;
The fopen() function opens a stream for use and links a file with that stream returning a valid file pointer which is positioned correctly within the file if all is correct. fopen() has the following prototype
where filename is a pointer to a string of characters which make up the name and path of the required file, and mode is a pointer to a string which specifies how the file is to be opened. The following table lists some values for mode.
opens a text file for reading (must exist)
opens a text file for writing (overwritten or created)
append to a text file
opens a binary file for reading
opens a binary file for writing
appends to a binary file
opens a text file for read/write (must exist)
opens a text file for read/write
append a text file for read/write
opens a binary file for read/write
opens a binary file for read/write
append a binary file for read/write
If fopen( ) cannot open "test.dat " it will a return a NULL pointer which should always be tested for as follows.