home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / unix / programm / 4028 < prev    next >
Encoding:
Text File  |  1992-07-30  |  1.2 KB  |  42 lines

  1. Newsgroups: comp.unix.programmer
  2. Path: sparky!uunet!ferkel.ucsb.edu!taco!dwthoma1
  3. From: dwthoma1@eos.ncsu.edu (DAVID W THOMAS)
  4. Subject: Re: Better way to find out the number of lines in a file
  5. Message-ID: <1992Jul31.045401.11291@ncsu.edu>
  6. Originator: dwthoma1@c00081-100lez.eos.ncsu.edu
  7. Lines: 29
  8. Sender: news@ncsu.edu (USENET News System)
  9. Reply-To: dwthoma1@eos.ncsu.edu (DAVID W THOMAS)
  10. Organization: North Carolina State University, Project Eos
  11. Date: Fri, 31 Jul 1992 04:54:01 GMT
  12.  
  13.  
  14. I tried e-mail to you, but it bounced.
  15.  
  16. You can find out the number of _characters_ with the following code, but
  17. there is no way to count the number of lines except to loop through
  18. the characters and count which are '\n' characters.  All files look
  19. like data to unix.  Unless you store the number of lines at the top or
  20. in another index file, unix is not going to store it for you.  
  21.  
  22. If your big file has a constant number of  characters per line, then
  23. you could use the following code and divide by the characters per line.
  24.  
  25.  
  26. #include <stdio.h>
  27.  
  28. main()
  29. {
  30.     FILE *fptr;
  31.     int size;
  32.  
  33.     fptr = fopen("big_file", "r");
  34.     fseek(fptr, 0, SEEK_END);
  35.     size = ftell(fptr);
  36.  
  37.     printf("the size is %d\n", size);
  38. }
  39.  
  40. David W. Thomas
  41. dwthoma1@eos.ncsu.edu
  42.