The fprintf and fscanf Functions


The functions fprintf and fscanf perform I/O operations that are identical to the familiar printf and scanf functions, except of course that they work on files. The first argument of these functions is a file pointer which specifies the file to be used. The general form of fprintf is


where fp is a file pointer associated with a file that has been opened for writig. The control string contains output specifications for the item in the list. The list may include variables, constants and strings. For example:
                                    fprintf ( f1, “%s %d %f”, name, age, 7.5 );
Here, name is an array variable of type char and age  is an int variable.
The general form of fscanf is


The statement would cause the reading of the items in the list from the file specified by fp, according to the specifications contained in the control string. Example:
                                    fscanf ( f2, “%s %d”, item, &quantity ) ;
Like scanf,fscanf also returms the number of items that are successfully read. When the end of file is reached, it returns the value of EOF.

Previous                                                                                    Next


Powered by Blogger.