I/ O Funtions

 

Line Input and Output with Files

The functions for line input-output are:

fgets();   /* ( similar to gets() ) */

fputs();   /* ( similar to puts() ) */

 

To use the fgets() function, the maximum line length in the file must be known. Assume that the longest line length in the file a.txt is 10 characters.

Consider the following code for copying line by line:

/* line by line copying of files */

#include <stdio.h>

main()

{

   :

   :

   char niv[11];              /* line read into niv */

 

  while (( fgets (niv, 11, ptr )) != NULL )       

                                    /* read into niv from ptr - pointer to a.txt */

 

    fputs (inp, ptr2);          /* ptr2 - pointer to b.txt */

    :

}

 

Formatted Input and Output with Files

Files in which each record has a fixed format can also be read or created in C using the following functions:

fscanf();        /* ( similar to scanf() ) */

fprintf();       /* ( similar to printf() ) */

 

The additional parameter that has to be specified with these functions is the pointer to the file to be used for input-output. The file pointer is the first parameter of both  functions, the remaining are identical to those of scanf() and printf() respectively.

In case the end-of-file is encountered during the read, fscanf() returns the value EOF.

 

The fseek() function

An input or output operation on a file results in shift in the current position on the file. The current position on a file is the next byte position from where data will be read in an input operation or written to in an output operation. The current position gets incremented by the number of bytes read or written. When the file is opened, the current position of the  file is 1, i.e. at the beginning of the file. A  position beyond the last byte in the file indicates end-of-file .

The function fseek() is used for repositioning the current position on a file opened by the function fopen().

 The syntax of the function is

rtn = fseek( file-pointer, offset, from-where );

where:

int rtn                      is the value returned by the function fseek(); 0 if successful and 1 if unsuccessful.

FILE file-pointer      is the pointer to the file.

long offset               is the number of bytes that the current position will shift on a file.

int from-where        is the position on the file from where the offset would be effective.Three values it can take are:

0   Offset is effective from beginning of file.

1   Offset is effective from current position.

2    Offset is effective from end-of-file.

 

The rewind() Function

The function rewind() is used to reposition the current position to the beginning of a file . It is useful for reinitialising the current position on a file.

The syntax of the function is:

rewind (file-pointer);

Where FILE file-pointer   is a pointer to a file.

After the function rewind()  is performed, the current position is    always 1, i.e. at the beginning of the file.

 

 

 

 

 

 

 

Copyright © 2008 Manikkumar. All rights reserved.