home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / c / other / file / records1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-25  |  2.3 KB  |  88 lines

  1. /* RECORDS1.C illustrates reading and writing of file records using seek
  2.  * functions including:
  3.  *      fseek       rewind      ftell
  4.  *
  5.  * Other general functions illustrated include:
  6.  *      tmpfile     rmtmp       fread       fwrite
  7.  *
  8.  * Also illustrated:
  9.  *      struct
  10.  *
  11.  * See RECORDS2.C for a version program using fgetpos and fsetpos.
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include <io.h>
  16. #include <string.h>
  17.  
  18. /* File record */
  19. struct RECORD
  20. {
  21.     int     integer;
  22.     long    doubleword;
  23.     double  realnum;
  24.     char    string[15];
  25. } filerec = { 0, 1, 10000000.0, "eel sees tar" };
  26.  
  27. main()
  28. {
  29.     int c, newrec;
  30.     size_t recsize = sizeof( filerec );
  31.     FILE *recstream;
  32.     long recseek;
  33.  
  34.     /* Create and open temporary file. */
  35.     recstream = tmpfile();
  36.  
  37.     /* Write 10 unique records to file. */
  38.     for( c = 0; c < 10; c++ )
  39.     {
  40.         ++filerec.integer;
  41.         filerec.doubleword *= 3;
  42.         filerec.realnum /= (c + 1);
  43.         strrev( filerec.string );
  44.  
  45.         fwrite( &filerec, recsize, 1, recstream );
  46.     }
  47.  
  48.     /* Find a specified record. */
  49.     do
  50.     {
  51.         printf( "Enter record betweeen 1 and 10 (or 0 to quit): " );
  52.         scanf( "%d", &newrec );
  53.  
  54.         /* Find and display valid records. */
  55.         if( (newrec >= 1) && (newrec <= 10) )
  56.         {
  57.             recseek = (long)((newrec - 1) * recsize);
  58.             fseek( recstream, recseek, SEEK_SET );
  59.  
  60.             fread( &filerec, recsize, 1, recstream );
  61.  
  62.             printf( "Integer:\t%d\n", filerec.integer );
  63.             printf( "Doubleword:\t%ld\n", filerec.doubleword );
  64.             printf( "Real number:\t%.2f\n", filerec.realnum );
  65.             printf( "String:\t\t%s\n\n", filerec.string );
  66.         }
  67.     } while( newrec );
  68.  
  69.     /* Starting at first record, scan each for specific value. The following
  70.      * line is equivalent to:
  71.      *      fseek( recstream, 0L, SEEK_SET );
  72.      */
  73.     rewind( recstream );
  74.  
  75.     do
  76.     {
  77.         fread( &filerec, recsize, 1, recstream );
  78.     } while( filerec.doubleword < 1000L );
  79.  
  80.     recseek = ftell( recstream );
  81.     /* Equivalent to: recseek = fseek( recstream, 0L, SEEK_CUR ); */
  82.     printf( "\nFirst doubleword above 1000 is %ld in record %d\n",
  83.             filerec.doubleword, recseek / recsize );
  84.  
  85.     /* Close and delete temporary file. */
  86.     rmtmp();
  87. }
  88.