How to Perform Input/output Operations on Files


Once a file is opened, reading out or writing to it accomplished using the standard routines listed in previous topic.

The getc and putc Functions


The simplest file I/O functions are getc and putc. These are analogous to getchar and putchar functions and handle one character at a time. Assume that a file is opened with mode w  and file pointer fp1. Then, the statement
                                    putc ( c, fp1 ) ;
writes the character contained in the character variable c to the file associated with FILE pointer fp1. Similarly, getc is used to read a character from a file that has been opened in read mode. For example, the statement
                                    c = getc ( fp2 ) ;
would read a character from the file whose file pointer is fp2.

The file pointer moves by one character position for every operation of getc or putc. The getc will return an end-of-file marker EOF, when end of the file has been reached. Therefore, the reading should be terminated when EOF is encountered.

The getw and putw Functions


The getw and putw are integer oriented functions. They are similar to the getc and putc functions and are used to read and write integer values. These functions would be useful when we deal with only integer data. The general form of getw and putw are:



Previous                                                                                    Next


Powered by Blogger.