home *** CD-ROM | disk | FTP | other *** search
/ C by Discovery (4th Edition) / C_By_Discovery_4th_Edition.tar / C_By_Discovery_4th_Edition / _DISK_ / ch10 / fread.c < prev    next >
C/C++ Source or Header  |  2005-06-16  |  3KB  |  97 lines

  1. /*               fread.c
  2.  *
  3.  *   Synopsis  - Reads transactions (elements of type struct 
  4.  *               trans) from a file and displays them on 
  5.  *               standard output.
  6.  *
  7.  *   Objective - To illustrate the use of fread() with structures.
  8.  */
  9.  
  10. /* Include Files */
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13.  
  14. /* Constant Definitions */
  15. #define BUF_SIZE  80
  16.  
  17. /* Type Descriptions */
  18. struct trans {
  19.      int t_type;
  20.      char payee_memo[BUF_SIZE];
  21.      double amount;
  22.      unsigned tax_deduct:1;
  23.      unsigned cleared:1;
  24. };
  25.  
  26. /* Function Prototypes */
  27. void print_trans( struct trans *outtrans );
  28. /*   PRECONDITION:  The parameter outtrans contains the address of a 
  29.  *                  struct trans declared by the calling function.
  30.  *
  31.  *   POSTCONDITION: Displays the contents of the structure pointed to 
  32.  *                  by outtrans on standard output. Each field is 
  33.  *                  separated with a vertical bar character.
  34.  */
  35.  
  36. int read_trans( struct trans *trans_ptr, FILE *fp );
  37. /*   PRECONDITION:  The parameter trans_ptr contains the address of a 
  38.  *                  struct trans declared by the calling function. fp
  39.  *                  contains the address of a FILE *.
  40.  *
  41.  *   POSTCONDITION: Reads a single transaction from the file 
  42.  *                  associated with fp and stores it in the structure 
  43.  *                  pointed to by trans_ptr.
  44.  */
  45.  
  46. int main( void )
  47. {
  48.      struct trans transact;
  49.      FILE *fp;
  50.  
  51.      if (( fp = fopen( "transact", "r" )) == NULL ) {
  52.           printf( "Transaction file couldn't be opened.\n" );
  53.           exit( 1 );
  54.      }
  55.  
  56.      while ( read_trans( &transact, fp ))            /* Note 1 */
  57.           print_trans( &transact );
  58.      fclose( fp );
  59.      return 0;
  60. }
  61.  
  62. /*******************************read_trans()********************/
  63.  
  64. int read_trans( struct trans *trans_ptr, FILE *fp )
  65. {
  66.      int retval;
  67.                                                      /* Note 2 */
  68.      retval = fread( trans_ptr, sizeof( *trans_ptr ), 1, fp );
  69.      return ( retval );
  70. }
  71.  
  72. /*******************************print_trans()*******************/
  73.  
  74. void print_trans( struct trans *outtrans )
  75. {
  76.      /* Cleared field */
  77.      if ( outtrans->cleared )
  78.           printf( "C | " );
  79.      else
  80.           printf( "  | " );
  81.  
  82.      /* Transaction type */
  83.      if (( outtrans->t_type =='D' )  || ( outtrans->t_type == 'W' ) || ( outtrans->t_type == 'I' ))
  84.           printf( "%4c | ", outtrans->t_type );
  85.      else
  86.           printf( "%4d | ", outtrans->t_type );
  87.  
  88.      /* tax_deduct field */
  89.      if ( outtrans->tax_deduct )
  90.           printf( " T | " );
  91.      else
  92.           printf( "   | " );
  93.  
  94.      printf( "%10.2f  | ", outtrans->amount );
  95.      printf( "%s", outtrans->payee_memo );
  96. }
  97.