C Programming

File handling example of a goods re-ordering program
The following program handles an ASCII text file which describes a number of products, and reads each product into a structure with the program.


/* File handling example for PR101     */
/* processing an ASCII file of records */
/* Written by B. Brown, April 1994     */
/*                                     */

/* process a goods file, and print out */
/* all goods where the quantity on     */
/* hand is less than or equal to the   */
/* re-order level.                     */


#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>

/* definition of a record of type goods */
struct goods {
   char  name[20];    /* name of product     */
   float price;       /* price of product    */
   int   quantity;    /* quantity on hand    */
   int   reorder;     /* re-order level      */
};

/* function prototypes */
void myexit( int );
void processfile( void );
void printrecord( struct goods );
int getrecord( struct goods * );

/* global data variables */
FILE *fopen(), *input_file;  /* input file pointer */

/* provides a tidy means to exit program gracefully */
void myexit( int exitcode )
{
   if( input_file != NULL )
      fclose( input_file );
   exit( exitcode );
}

/* prints a record */
void printrecord( struct goods record )
{
   printf("\nProduct name\t%s\n", record.name );
   printf("Product price\t%.2f\n", record.price );
   printf("Product quantity\t%d\n", record.quantity );
   printf("Product reorder level\t%d\n", record.reorder );
}

/* reads one record from inputfile into 'record', returns 1 for success */
int getrecord( struct goods *record )
{
   int loop = 0, ch;
   char buffer[40];

   ch = fgetc( input_file );
   /* skip to start of record */
   while( (ch == '\n') || (ch == ' ') && (ch != EOF) )
      ch = fgetc( input_file );
   if( ch == EOF ) return 0;

   /* read product name */
   while( (ch != '\n') && (ch != EOF)) {
      buffer[loop++] = ch;
      ch = fgetc( input_file );
   }
   buffer[loop] = 0;
   strcpy( record->name, buffer );
   if( ch == EOF ) return 0;

   /* skip to start of next field */
   while( (ch == '\n') || (ch == ' ') && (ch != EOF) )
      ch = fgetc( input_file );
   if( ch == EOF ) return 0;

   /* read product price */
   loop = 0;
   while( (ch != '\n') && (ch != EOF)) {
      buffer[loop++] = ch;
      ch = fgetc( input_file );
   }
   buffer[loop] = 0;
   record->price = atof( buffer );
   if( ch == EOF ) return 0;

   /* skip to start of next field */
   while( (ch == '\n') || (ch == ' ') && (ch != EOF) )
      ch = fgetc( input_file );
   if( ch == EOF ) return 0;

   /* read product quantity */
   loop = 0;
   while( (ch != '\n') && (ch != EOF)) {
      buffer[loop++] = ch;
      ch = fgetc( input_file );
   }
   buffer[loop] = 0;
   record->quantity = atoi( buffer );
   if( ch == EOF ) return 0;

   /* skip to start of next field */
   while( (ch == '\n') || (ch == ' ') && (ch != EOF) )
      ch = fgetc( input_file );
   if( ch == EOF ) return 0;

   /* read product reorder level */
   loop = 0;
   while( (ch != '\n') && (ch != EOF)) {
      buffer[loop++] = ch;
      ch = fgetc( input_file );
   }
   buffer[loop] = 0;
   record->reorder = atoi( buffer );
   if( ch == EOF ) return 0;

   return 1;  /* signify record has been read successfully */
}

/* processes file for records */
void processfile( void )
{
   struct goods record;   /* holds a record read from inputfile */

   while( ! feof( input_file )) {
      if( getrecord( &record ) == 1 ) {
         if( record.quantity <= record.reorder )
            printrecord( record );
      }
      else myexit( 1 );  /* error getting record */
   }
}


main()
{
   char filename[40];     /* name of database file */

   printf("Example Goods Re-Order File Program\n");
   printf("Enter database file ");
   scanf(" %s", filename );
   input_file = fopen( filename, "rt" );
   if( input_file == NULL ) {
      printf("Unable to open datafile %s\n", filename );
      myexit( 1 );
   }
   processfile();
   myexit( 0 );
}

The datafile (a standard ASCII text file) used for this example looks like

baked beans
1.20
10
5

greggs coffee
2.76
5
10

walls ice-cream
3.47
5
5

cadburys chocs
4.58
12
10

©Copyright B Brown. 1984-1999. All rights reserved.