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 / writef2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-01-07  |  1.2 KB  |  38 lines

  1. /* WRITEF2.C - From page 459 of "Microsoft C Programming for    */
  2. /* the IBM" by Robert Lafore. This program writes formatted     */
  3. /* data to a file. To use, enter data at prompt as follows:     */
  4. /*   Bond 007 74.5 <return>.   To exit after entering desired   */
  5. /* number of lines, enter at prompt:   x 0 0 <return>.          */
  6. /* This program will not work correctly if the DOS type comm-   */
  7. /* and is used to read it. To read the file, use READF.C, also  */
  8. /* on this disk. The file WRITEF_A.C on this disk can be read   */
  9. /* with the DOS type command.                                   */
  10. /****************************************************************/
  11.  
  12. #include <stdio.h>
  13.  
  14. main()
  15. {
  16. FILE *fptr;
  17. char name[40];
  18. int code;
  19. float height;
  20.  
  21.    if((fptr = fopen("c:textfile.txt", "w")) == NULL) {
  22.       printf("\nCan't open textfile.txt");
  23.       exit();
  24.    }
  25.    do {
  26.       printf("Type name, code number, and height: ");
  27.       scanf("%s %d %f", name, &code, &height);
  28.       fprintf(fptr, "%s %d %f", name, code, height);
  29.       if (ferror(fptr)) {
  30.          perror("Write error");
  31.          fclose(fptr);
  32.          exit();
  33.       }
  34.    } while(strlen(name) > 1);              /* no name given ? */
  35.    fclose(fptr);
  36. }
  37.  
  38.