home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / R_LA4_02.ZIP / WRITER.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-01-06  |  1.2 KB  |  40 lines

  1. /* WRITER.C - From page 448 of "Microsoft C Programming for     */
  2. /* the IBM" by Robert Lafore. Writes structures to a file us-   */
  3. /* ing the record I/O method of performing standard I/O. Ano-   */
  4. /* ther name for record I/O is block I/O. Record I/O writes to  */
  5. /* files in binary. NOTE!!!! This program will not link cor-    */
  6. /* rectly with Quick C. It gives Fatal error L1101 & manual     */
  7. /* recommends contacting Microsoft.                             */
  8. /****************************************************************/
  9.  
  10. #include "stdio.h"
  11.  
  12. main()
  13. {
  14. struct {
  15.    char name[40];
  16.    int agnumb;
  17.    float height;
  18. } agent;
  19. float dummy = 0.0;
  20. FILE *fptr;
  21.  
  22.    if((fptr = fopen("agents.rec", "wb")) == NULL) {
  23.       printf("\nCan't open file agents.rec");
  24.       exit();
  25.    }
  26.    do {
  27.       printf("\nEnter name: ");
  28.       gets(agent.name);
  29.       printf("Enter number: ");
  30.       scanf("%d", &agent.agnumb);
  31.       printf("Enter height: ");
  32.       scanf("%f", &agent.height);
  33.       fflush(stdin);                   /*flush keyboard buffer*/
  34.       fwrite(&agent, sizeof(agent), 1, fptr);
  35.       printf("Add another agent (y/n) ? ");
  36.    } while(getche() == 'y');
  37.    fclose(fptr);
  38. }
  39.  
  40.