Thread: generating program statistics

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    8

    generating program statistics

    hi,

    Im trying a program which reads in the source of a C program from its standard input and prints out the following statistics for the program (all as integers)

    total number of lines
    total number of blank lines
    total number of characters
    total number of spaces

    Suggestions plz anyone?

  2. #2
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    You need fgets, a couple of functions out of the ctype.h library and a loop or two.

    Go forth and code!
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    8
    I've done this code but still can't find a way of counting the total number of blank lines.

    Code:
    main()
    {
    clrscr();
    FILE *fp;
    char ch;
    int nol=0,not=0,nob=0,noc=0;
    
    fp=fopen("filename.cpp","r");
    
    while(1)
    {
    ch=fgetc(fp);
    
    if(ch==EOF)
    break;
    
    noc++;
    
    if(ch==' ')
    nob++;
    
    if(ch=='\n')
    nol++;
    
    if(ch=='\t')
    not++;
    }
    
    fclose(fp);
    noc=noc-nob;
    printf("\nNumber of characters = %d",noc);
    printf("\nNumber of blanks = %d",nob);
    printf("\nNumber of tabs = %d",not);
    printf("\nNumber of lines = %d",nol);
    getch();
    }

  4. #4
    Registered User
    Join Date
    Dec 2005
    Location
    Australia - Melbourne
    Posts
    63
    if i'm not mistaken, two consecutive newline characters is a blank line.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by peterchen
    if i'm not mistaken, two consecutive newline characters is a blank line.
    That depends on what you consider a blank line.
    Code:
    Here is a line.
        
    Here is another line.
    One might say there is a blank line separating those two statements, but there isn't by your standards. So to be more specific... two newlines with white space or nothing in between them.
    Sent from my iPadŽ

  6. #6
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    This is more what I was thinking about. Though, it will suffer from the small bug that sly mentioned above.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
    
        FILE *fp;
        char buf[BUFSIZ];
        char ch;
        int nol = 0, not = 0, nob = 0, noc = 0, no_empty = 0;
        int i;
        
        if ( (fp = fopen("filename.cpp", "r")) == NULL )
        {
             printf("Could not open file for reading.\n\n");
             exit(1);
        }
        
        while ( (fgets( buf, BUFSIZ, fp) != '\0') )
        {
              
              for ( i = 0; (ch = buf[i]) != '\0'; i++ )
              {
                    if ( i == 0 && ch == '\n' )
                    {
                         nol++;
                         noc++;
                         no_empty++;
                         break;
                    }
                    
                    switch ( ch )
                    {
                           case ' ':
                                nob++;
                                noc++;
                                break;
                           case '\n':
                                nol++;
                                noc++;
                                break;
                           case '\t':
                                not++;
                                noc++;
                                break;
                           default:
                                noc++;
                    }
              }
        }
        
        printf("No. of chars: %d\n", noc);
        printf("No. of spaces: %d\n", nob);
        printf("No. of lines: %d\n", nol);
        printf("No. of tabs: %d\n", not);
        printf("No. of blank lines: %d\n", no_empty);
        
        system("PAUSE");	
        return 0;
    }
    Last edited by bivhitscar; 07-02-2006 at 07:23 AM.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  7. #7
    Registered User
    Join Date
    Jun 2006
    Posts
    8

    Thumbs up

    bivhitscar, thanks a lot its working fine now

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > fgets( buf, BUFSIZ, fp) != '\0'
    Should be != NULL
    '\0' implies the nul character
    NULL implies the NULL pointer.
    The compiler will ultimately do the right thing, but it sends the wrong impression to the reader.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Bugger; thanks salem. The first time I wrote it out, it came out as NULL - but for some reason I thought it was wrong and changed it.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  10. #10
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Quote Originally Posted by peterchen
    if i'm not mistaken, two consecutive newline characters is a blank line.
    And gcc throws warnings is a source file doesn't end in a blank line. And according to your definition, that last line won't get counted. Take this file end: "#endif\n" (Then EOF) There is a final blank line, but no consecutive \n's since it's the last line.

    Read in a line at a time, and if it contains nothing or spaces, tabs, \n, etc - depending on what you consider blank - then it's blank.

    Edit: All the code so far seems to be one line off.
    Last edited by Cactus_Hugger; 07-02-2006 at 12:13 PM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generating data file names from program variables
    By a380x in forum C Programming
    Replies: 2
    Last Post: 12-18-2006, 12:12 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM