/* ************************************************************************ */
/*                      include file for file I/O                           */
/*                                                                          */
/*  Needed in order to perform any kind of I/O except `printf' & `scanf'.   */
/*  Contains all I/O functions and definitions for proper file I/O and file */
/*  I/O validation.                                                         */
/* ************************************************************************ */

#include <stdio.h>

/* ************************************************************************ */
/*                      file pointer definition statement                   */
/*                                                                          */
/*  Defines `fptr' to be a pointer to a type of FILE.                       */
/* ************************************************************************ */
                                                                                    
FILE *fptr;                                                                       
                                                                                   
/* ************************************************************************ */
/*                              fopen function                              */
/*                                                                          */
/*  Opens a file for I/O depending on the `mode' (the second argument).     */
/*  The first argument is the file name.  Both arguments must be a pointer  */
/*  to a character string.                                                  */
/* ************************************************************************ */

fptr = fopen ( "file_name", "wt");

/* ************************************************************************ */
/*                              validation code                             */
/*                                                                          */
/*  This code validates the `fopen' function to see whether the file was    */
/*  opened successfully for I/O.  If it was not opened successfully it      */
/*  returns a NULL pointer.                                                 */
/* ************************************************************************ */

if ( fptr  ==  NULL )
 {
   fprintf (stderr, "Could not open <%s> for writing.\n", "file_name");
   exit(1);
 }                                                                                           
                                                                                            
/* ************************************************************************ */
/*  formatted I/O TO a file on disk                                         */
/* ************************************************************************ */

/*  if your record looks like this  */
 
struct record                                                                           
{
 char prod_name[20];
 int prod_num;
 float prod_price;
 int prod_in_stock;
};
 
struct record out_rec;
 
/*  then your formatted I/O statement could look like this  */
 
fprintf (fptr, "%-20s %3d %3d %3d %3d\n", out_rec.prod_name, 
                                          out_rec.prod_num,
                                          out_rec.prod_price,
                                          out_rec.prod_in_stock);
 
/* ************************************************************************ */
/*  formatted I/O FROM a file on disk                                       */
/* ************************************************************************ */

/*  if your record looks like this  */
 
struct record                                                                           
{
 char prod_name[20];
 int prod_num;
 float prod_price;
 int prod_in_stock;
};
 
struct record out_rec;
 
/*  then your formatted I/O statement could look like this  */
 
fscanf  (fptr, "%20s %3d %3d %3d %3d\n",  out_rec.prod_name, 
                                         &out_rec.prod_num,
                                         &out_rec.prod_price,
                                         &out_rec.prod_in_stock);
 
/* ************************************************************************ */
/*  sample use of the `ftell' and `fseek' functions to update a file        */
/* ************************************************************************ */

long foff;    /* long int used to save the offset in the file  */
 
struct record                                                                           
{
 char prod_name[20];
 int prod_num;
 float prod_price;
 int prod_in_stock;
};
 
struct record out_rec;
 
foff = ftell(fptr);  /*  saves the offset of the next record to be read from the file  */
 
fscanf  (fptr, "%20s %3d %3d %3d %3d\n",  out_rec.prod_name, 
                                         &out_rec.prod_num,
                                         &out_rec.prod_price,
                                         &out_rec.prod_in_stock);
 
fseek(fptr, foff, SEEK_SET); /*  resets the file pointer to the record saved in the ftell  */
 
fprintf (fptr, "%-20s %3d %3d %3d %3d\n", out_rec.prod_name, 
                                          out_rec.prod_num,
                                          out_rec.prod_price,
                                          out_rec.prod_in_stock);
 
/*  the third argument of the `fseek' can be one of the following  */
 
SEEK_SET or 0  /*  offsets from the beginning of the file  */
SEEK_CUR or 1  /*  offsets from the current position in the file  */
SEEK_END or 2  /*  offsets from the end of the file  */
