home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / alde_c / misc / lib / r_la4_02 / randr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-01-07  |  988 b   |  37 lines

  1. /* RANDR.C - From page 456 of "Microsoft C Programming for      */
  2. /* the IBM" by Robert Lafore. Reads one agent's record, sel-    */
  3. /* ected by user, from database created by AGENTR.C             */
  4. /****************************************************************/
  5.  
  6. #include "stdio.h"
  7.  
  8. main()
  9. {
  10. struct {
  11.    char name[40];
  12.    int agnumb;
  13.    float height;
  14. } agent;
  15. FILE *fptr;
  16. int recno;
  17. long int offset;
  18.  
  19.    if ((fptr = fopen("c:agents.rec", "r")) == NULL) {
  20.       printf("\nCan't open file agents.rec");
  21.       exit();
  22.    }
  23.    printf("\nEnter record number: ");
  24.    scanf("%d", &recno);
  25.    offset = --recno * sizeof(agent);        /*find offset*/
  26.    if(fseek(fptr, offset, 0) != 0) {      /*go there*/
  27.       printf("\nCan't move pointer there.");
  28.       exit();
  29.    }
  30.    fread(&agent, sizeof(agent), 1, fptr);
  31.    printf("\nName: %s", agent.name);
  32.    printf("\nNumber: %03d", agent.agnumb);
  33.    printf("\nHeight: %.2f", agent.height);
  34.    fclose(fptr);
  35. }
  36.  
  37.