The Stream File in C Programming Language

Although C does not have any built-in method of performing file I/O, the C standard library contains a very rich set of I/O functions providing an efficient, powerful and flexible approach. We will cover the ANSI file system but it must be mentioned that a second file system based upon the original UNIX system is also used but not covered on this course.

A very important concept in C is the stream. In C, the stream is a common, logical interface to the various devices that comprise the computer. In its most common form, a stream is a logical interface to a file. As C defines the term "file", it can refer to a disk file, the screen, the keyboard, a port, a file on tape, and so on. Although files differ in form and capabilities, all streams are the same. The stream provides a consistent interface and to the programmer one hardware device will look much like another.

stream is linked to a file using an open operation. A stream is disassociated from a file using a close operation. The current location, also referred to as the current position, is the location in a file where the next file access will occur. There are two types of streams: text (used with ASCII characters some character translation takes place, may not be one-to-one correspondence between stream and what's in the file) andbinary (used with any type of data, no character translation, one-to-one between stream and file).

To open a file and associate it with a stream, use fopen(). Its prototype is shown here:

FILE *fopen(char *fname,char *mode);

The fopen() function, like all the file-system functions, uses the header stdio.h . The name of the file to open is pointed to by fname (must be a valid name). The string pointed at 
for mode determines how the file may be accesed as shown:


ModeMeaning
rOpen a text file for reading
wCreate a text file for writing
aAppend to a text file
rbOpen a binary file for reading
wbOpen a binary file for writing
abAppend to a binary file
r+Open a text file for read/write
w+Create a text file for read/write
a+Append or create a text file for read/write
r+bOpen a binary file for read/write
w+bCreate a binary file for read/write
a+bAppend a binary file for read/write


If the open operation is successful, fopen() returns a valid file pointer. The type FILE is defined in stdio.h. It is a structure that holds various kinds of information about the file, such as size.The file pointer will be used with all other functions that operate on the file and it must never be altered or the object it points to. If fopen() fails it returns a NULL pointer so this must always be checked for when opening a file. For example:

FILE *fp;

if ((fp = fopen("myfile", "r")) ==NULL){

printf("Error opening file\n");

exit(1);

}

To close a file, use fclose(), whose prototype is

int fclose(FILE *fp);

The fclose() function closes the file associated with fp, which must be a valid file 
pointer previously obtained using fopen(), and disassociates the stream from the file. The fclose() function returns 0 if successful and EOF(end of file) if an error occurs.
Once a file has been opened, depending upon its mode, you may read and/or write bytes to or from it using these two functions.

int fgetc(FILE *fp);

int fputc(int ch, FILE *fp);

The getc() function reads the next byte from the file and returns its as an integer and if error occurs returnsEOF. The getc() function also returns EOF when the end of file is reached. Your routine can assign fget()'s return value to a char you don't have to assign it to an integer.
The fput() function writes the bytes contained in ch to the file associated with fp as an unsigned char. Althoughch is defined as an int, you may call it using simply a char. The fput() function returns the character written if successful or EOF if an error occurs.


Learn More :