count only lines of code...

This is a discussion on count only lines of code... within the C Programming forums, part of the General Programming Boards category; Question: How can I only count the lines of code in a source file and ignore the comment lines and ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    27

    Question count only lines of code...

    Question: How can I only count the lines of code in a source file and ignore the comment lines and blank lines?
    For example, if I have a source file as follows how would I only count the lines as described above.
    Thanks!
    Flight

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    
    //declarations
    char file_name[100], line1[10] ;
    FILE *inp1, *out1 ;
    int count, update_counter ;
    
       // init some stuf
       count=0 ;
       update_counter = 0 ;
    
        // prompt the user
        printf("Enter Filename : ")  ;
    
       // read the keyboard input
        fgets(file_name, sizeof(file_name), stdin);
    
       //remove the newline character from the end of the filename
       file_name[strlen(file_name) - 1] = '\0' ;
    
       // open input file
       inp1 = fopen(file_name, "r") ;
    
        if(inp1 == NULL)	
       {
          fprintf(stderr, "can't open %s\n", file_name) ;
          exit(EXIT_FAILURE) ;
       }
    
       // open output file #1
       out1 = fopen("output1.txt", "w") ;
    
        if(out1 == NULL)	
       {
           fprintf(stderr, "can't open %s\n", "output1.txt") ;
           exit(EXIT_FAILURE) ;
       }
    
        // count the words in the input file
        for ( ;; )
        {
           int ch = fgetc(inp1);
           if ( ch == EOF )
           {
              break;
           }
           if ( isalnum(ch) || ispunct(ch))
           {
              update_counter = 1;
    
           }
           else if ( isspace(ch) && update_counter )
           {
              count++ ;
              update_counter = 0;
           }
        }
    
        //for those files that end with lines with no line feeds
        //this will increment the counter one last time...
        if (update_counter)
        {
    	  count++;
        }
     
        //write the word count to the output file
        fprintf(out1,"File %s contains %d words \n", file_name, count) ;
    
        // we're done...close files
        fclose(inp1) ;
        fclose(out1) ;
    
        return 0;
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    What part are you having trouble with?

    Read a line, determine its type, and either count it or don't. Remember that:
    - Comments and code can exist on the same line
    - Comments can run across multiple lines
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    27
    I would want to exclude those lines which are blank and those that are entirely a comment line. Just not sure how to do this.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Have you managed to open the file, and read a line in?

    A line is empty if all it's characters meet the criteria for function isspace().
    http://www.rt.com/man/isspace.3.html

    Parse the line you've just read and check each character against that function. If they all return true, the line can be deemed empty.

    To be a comment line (just comment, no code), the first non-white space characters need to be /* or //. Depending on which it is, the last non-white space character may need to be */. Again, some buffer parsing is needed for this. (watch out for the multi-line comments).

    This is one small part in a much bigger design. Think about how you might fit it all together (on paper) before you start coding.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    twm
    twm is offline
    root
    Join Date
    Sep 2003
    Posts
    232
    First you need to strip leading whitespace, that's easy so I won't go into it. Then do something like this:
    Code:
    if (string[0] == '/' && (string[1] == '*' || string[1] == '/'))
      /* Comment */
    if (string[0] == '\0')
      /* Empty line */
    Okay, I'll go into stripping leading whitespace:
    Code:
    void *lws(char *string) {
      char *s;
    
      for (s = string; *s != '\0' && isspace(*s); s++)
        ;
      memmove(string, s, strlen(s) + 1);
    }
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  6. #6
    Registered User
    Join Date
    Sep 2003
    Posts
    27

    Wink

    good advice. All comment lines are of the syntax "//". Would I be wise to choose the function 'getc' to search for a new line '\n' followed by the '/' and then another '/'? or is there a better way to accomplish this?

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    27

    Wink

    twm...you kind of blew me away with the stripping of the white space..my previous reply was to hammer's I didn't see yours until after I replied. Any advice on my previous reply...either twm or hammer.

  8. #8
    twm
    twm is offline
    root
    Join Date
    Sep 2003
    Posts
    232
    >or is there a better way to accomplish this?
    There's no "good" way to work with strings in C, but it's usually better to use fgets to get a line and then work with it in memory. My last post assumed such a setup.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Read a complete line with fgets() and parse the buffer in memory.

    Depending on how you do this, you probably won't need to actually strip the leading white space, only advance the pointer to the beginning of the data. This saves the memmove() call in twm's sample.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    flightsimdude, start by opening and reading the file. Write everything you read to the screen. Once you've done this, you have a basis to move forward on. Post it when you've done if you want comments/advice or if you have trouble. When this stage is complete, then start to worry about comment lines etc.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Registered User
    Join Date
    Sep 2003
    Posts
    27
    ok I'm completely humbled...not that I needed any! How do I advance the pointer...or is there another simpler way to read through the file and ignore the blank lines and lines that are comment only type lines? I know I'm probably trying your patience. If I am I won't post any more questions.
    Thanks!
    Flight

  12. #12
    twm
    twm is offline
    root
    Join Date
    Sep 2003
    Posts
    232
    >This saves the memmove() call in twm's sample.
    >How do I advance the pointer...
    Well, if you want to do it the easy way...
    Code:
    char *sws(char *s) {
      while (isspace(*s))
        s++;
    
      return s;
    }
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  13. #13
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>I know I'm probably trying your patience.
    Not at all.

    Try doing as I said in my previous post. It's hard to help you if we don't know what level you're at, by seeing your code we can judge how to help you better.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  14. #14
    Registered User
    Join Date
    Sep 2003
    Posts
    27
    thanks guys. It's late for me now. Ill be back tomorrow after I try what you have conveyed to me. Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. switch memory locs with 2 lines of code
    By saltzmanjoelh in forum C++ Programming
    Replies: 12
    Last Post: 09-20-2008, 08:55 PM
  2. Checking lines of code
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 04-10-2008, 03:05 AM
  3. Replies: 5
    Last Post: 09-28-2004, 01:38 PM
  4. What is the syntax for a #define for several lines of code?
    By Jonas_Valleskog in forum C Programming
    Replies: 7
    Last Post: 01-31-2003, 12:22 PM
  5. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21