Most programs need to read and write data to disk-based storage systems. Word processors need to store text files, spreadsheets need to store the contents of cells, and database need to store records. This article explores the facilities that C offers for input and output (I/O) to a disk system.
The C language does not contain any explicit I/O statements. All I/O operations are carried out using functions from the standard C library. This approach makes the C file system powerful and flexible. I/O in C is unique because data may be transferred in its internal binary representation or in a human-readable text format. This makes it easy to create files to fit any need.
It is important to understand the difference between files and streams. The I/O system in C provides an interface to the user. This interface is independent of the actual device being accessed. This interface is not actually a file but an abstract representation of the device. This abstract interface is known as a stream and the actual device is called the file.
The C file system works with a wide variety of devices including printers, disk drives, tape drives and terminals. Even though all these devices are different from each other, the buffered file system transforms each device into a logical device called a stream. All streams act similarly and hence an ease can be brought about in handling these different types of devices. There are two types of streams- the text stream and binary stream.
A text stream is a sequence of characters, which can be organized into lines terminated by a newline character. However, the newline character is optional on the last line and is terminated by the implementation. In a text stream, certain character translations may occur as required by the environment. For example, a new line may be converted to a carriage return/linefeed pair. Therefore, there may not be a one-to-one relationship between the characters that are written (or read) and those in the external device. Also, because of possible translations, the number of characters written (or read) may not be the same as those in the external device.
A binary stream is a sequence of bytes with a one-to-one correspondence to those in the external device that is, there are no character translations. Also, the number of bytes written (or read) is the same as the number of bytes on the external device. Binary streams are a flat sequence of bytes, which do not have any flags to indicate the end of file or end of record. End-of-file is determined by the size of the file.
A file can be anything from a disk file to a terminal or a printer. However, all files do not have the same capabilities. For example, a disk file can support random access while a keyboard cannot.
A file can be associated with a stream by performing an open operation and likewise it can be disassociated from a stream by a close operation. All files are automatically closed when the program using them terminates, normally by main() returning to the operating system or by a call to exit(). Files are However, not closed when a program crashes.
Basic File Functions
The ANSI file system is composed of several interrelated functions. The most common once are listed in below table.
|fopen()||Opens a file|
|fclose()||Closes a file|
|putc()||Writes a character to a file (macro version)|
|fputc()||Writes a character from a file|
|getc()||Reads a character from a file|
|fgets()||Reads a string from a file|
|fseek()||Seeks to specified byte in a file|
|fprintf()||Is to a file what printf() is to the console|
|fscanf()||Is to a file what scanf() is to the console|
|feof()||Returns true if end-of-file is reached|
|ferror()||Returns true if an error has occured|
|rewind()||Resets the file position locator to the begining of the file|
|remove()||Erase a file|
|fflush()||Flushes a file|
These functions are contained in the header file stdio.h, and so require that this file be included in any program, which needs these functions. As can be seen, most of these functions are similar to the console I/O functions except for a prefix of the alphabet “f”. The stdio.h header file also defines several macros. An example of this is the EOF macro, which is defined as -1, and is the value returned when an input function tries to read paste the end of the file.
The remaining part is continued in next article
Comments/Suggestions are invited. Happy coding......!