home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / c / 17066 < prev    next >
Encoding:
Text File  |  1992-11-24  |  1.2 KB  |  49 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!almserv!antares!e3ubec
  3. From: e3ubec@fnma.COM (Blaine Crowther)
  4. Subject: Re: Looking for "C" Source Line Counter
  5. Message-ID: <1992Nov23.230453.9874@almserv.uucp>
  6. Sender: usenet@almserv.uucp
  7. Nntp-Posting-Host: antares
  8. Reply-To: e3ubec@fnma.COM
  9. Organization: Fannie Mae
  10. References: <1992Nov21.184741.6512@gw.wmich.edu>
  11. Date: Mon, 23 Nov 1992 23:04:53 GMT
  12. Lines: 35
  13.  
  14. If you can't find one try this:
  15.  
  16. void
  17. main( int argc, char * argv[])
  18. {
  19.      #define DELIMITER ';'
  20.  
  21.      int  ch,
  22.           line = 0;
  23.           
  24.      FILE * fp;
  25.      
  26.      if( argc != 2)
  27.           fprintf( stderr,"Please input a file name\n");
  28.      
  29.      else if( (fp = fopen( argv[1], "r")) == (FILE *)NULL)
  30.           fprintf( stderr,"File '%s' could not be opened\n",argv[1]);
  31.      else
  32.      {
  33.           while( (ch = getc( fp )) != EOF)
  34.                 if( DELIMITER == ch )
  35.                     line++;     
  36.           fprintf(stdout,"File %s contains %d lines.\n", argv[1], line);
  37.      }
  38. }
  39.  
  40. It's ansi , so it should work anywhere. Nothing fancy, it just counts
  41. semi-colons. If you want all lines, just redefine DELIMITER '\n' for
  42. new lines.
  43.  
  44. command line execution would be:
  45.  
  46. program_name file_name
  47.  
  48. blaine
  49.