Guest User

Untitled

a guest
Apr 7th, 2013
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.39 KB | None | 0 0
  1. /*
  2. ** File FILE_3.C
  3. **
  4. ** Illustrates how to read from a file.
  5. **
  6. ** The file is opened for reading.  Each line is successively fetched
  7. ** using fgets command.  The string is then converted to a long integer.
  8. **
  9. ** Note that fgets returns NULL when there are no more lines in the file.
  10. **
  11. ** In this example file ELAPSED.DTA consists of various elapsed times in
  12. ** seconds.  This may have been the result of logging the time  of events
  13. ** using an elapsed time counter which increments each second from the
  14. ** time the data logger was placed in service.
  15. **
  16. ** Typical data in elapsed.dta might be;
  17. **
  18. ** 65
  19. ** 142
  20. ** 1045
  21. ** 60493
  22. ** 124567
  23. */
  24.  
  25. #include <stdio.h>   /* required for file operations */
  26. #include <conio.h>  /* for clrscr */
  27. #include <dos.h>  /* for delay */
  28.  
  29. FILE *fr;            /* declare the file pointer */
  30.  
  31. main()
  32.  
  33. {
  34.    int n;
  35.    long elapsed_seconds;
  36.    char line[80];
  37.    clrscr();
  38.  
  39.    fr = fopen ("elapsed.dta", "rt");  /* open the file for reading */
  40.    /* elapsed.dta is the name of the file */
  41.    /* "rt" means open the file for reading text */
  42.  
  43.    while(fgets(line, 80, fr) != NULL)
  44.    {
  45.      /* get a line, up to 80 chars from fr.  done if NULL */
  46.      sscanf (line, "%ld", &elapsed_seconds);
  47.      /* convert the string to a long int */
  48.      printf ("%ld\n", elapsed_seconds);
  49.    }
  50.    fclose(fr);  /* close the file prior to exiting the routine */
  51. } /*of main*/
Advertisement
Add Comment
Please, Sign In to add comment