Counting Lines

This is a discussion on Counting Lines within the C Programming forums, part of the General Programming Boards category; Hi, Code: void check_activity(char * this_months, char * last_months) { FILE * fp1; FILE * fp2; int diff = 0; ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    93

    Counting Lines

    Hi,

    Code:
    void check_activity(char * this_months, char * last_months)
    {
    FILE * fp1;
    FILE * fp2;
    int diff = 0;
    int result = 0;
    int countA = 0, countB = 0;
    
    #ifdef DEBUG
        printf("In check_activity().\n");
        fflush(NULL);
    #endif
    
       if(!(fp1 = fopen(this_months, "r")))
          error_func("In check_activity().", "Error : fopen(INPUT_FILE)", SYS | FATAL);
    
       if(!(fp2 = fopen(last_months, "r")))
          error_func("In check_activity().", "Error : fopen(OUTPUT_FILE)", SYS | FATAL);
    /*
       while(fscanf(fp1, "%d", &countA) != NULL)
          ;
       while(fscanf(fp2, "%d", &countB) != NULL)
          ;
    */
       if(fp1 != NULL)
       {
          /* Read the number of records */
          fread(&countA, sizeof(int), 1, fp1);
          fclose(fp1);
       }
    
       if(fp2 != NULL)
       {
          /* Read the number of records */
          fread(&countB, sizeof(int), 1, fp2);
          fclose(fp2);
       }
    
       diff = countA - countB;
    
       fclose(fp1);
       fclose(fp2);
    
    printf("this month, last month, difference %d %d %d\n", countA, countB, diff);
    #ifdef DEBUG
        printf("Out check_activity().\n");
        fflush(NULL);
    #endif
    }
    I was hoping to establish the difference in the number of lines between two files, but as I have deliberately created two files which have 6 lines and 4 lines respectively, I was expecting an answer of 2

    As _you_ will be able to see, that is not happening, perhaps you could tell me why please ?



    tia,

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    98
    I don't think fread is going to help you here (though I may be wrong!!). I would have done it like this...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    FILE * fp1;
    FILE * fp2;
    int diff = 0;
    int result = 0;
    int countA = 0, countB = 0;
    char *this_months = "this_months";
    char *last_months = "last_months";
    char c;
    
       if(!(fp1 = fopen(this_months, "r")))
       {
         printf( "\nUnable to open fp1\n" );
         return 0;
       }
    
       if(!(fp2 = fopen(last_months, "r")))
       {
         printf( "\nUnable to open fp2\n" );
         return 0;
       }
    
       if(fp1 != NULL)
       {
          /* Read the number of records */
          while(( c = fgetc( fp1 )) != EOF )
            if( c == '\n' )
              countA++;
    
          fclose(fp1);
       }
    
       if(fp2 != NULL)
       {
          /* Read the number of records */
          while(( c = fgetc( fp2 )) != EOF )
            if( c == '\n' )
              countB++;
    
          fclose(fp2);
       }
    
       diff = countA - countB;
    
       fclose(fp1);
       fclose(fp2);
    
       printf("this month, last month, difference %d %d %d\n",
               countA, countB, diff);
    }
    Obviously, you'll need to convert it back into your function...

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    93
    ty Morgan


  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    char c;
    /* ... */
    while(( c = fgetc( fp1 )) != EOF )
    The function fgetc returns an int; EOF is a macro "which expands to an integer constant expression, with type int and a negative value" -- so it would be best to make c an int.

    [nitpicks]
    Code:
    if(!(fp1 = fopen(this_months, "r")))
    {
      printf( "\nUnable to open fp1\n" );
      return 0;
    }
    /* ... */
    if(fp1 != NULL)
    {
       /* ... */
    }
    There's nothing wrong with safety checks, but if you return from this function when fp1 is NULL, fp1 will never be NULL later on in the function. Same goes for fp2.
    Code:
    if(fp2 != NULL)
    {
       /* ... */
       fclose(fp2);
    }
    /* ... */
    fclose(fp1);
    fclose(fp2);
    You only need to close files that are open.

    [/nitpicks]
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    93
    Dave_Sinkula:
    Just Lurking:

    [nitpicks]
    if(fp1 != NULL)
    {
    /* ... */
    }
    There's nothing wrong with safety checks,
    but if you return from this function when fp1 is NULL,
    fp1 will never be NULL later on in the function. Same goes for fp2.

    sorry Dave, a little confused, are you saying that I should not use the if test like this

    Code:
       if(!(fp1 = fopen(this_months, "r")))
          error_func("In check_activity().", "Error : fopen(INPUT_FILE)", SYS | FATAL);
    
       if(!(fp2 = fopen(last_months, "r")))
          error_func("In check_activity().", "Error : fopen(OUTPUT_FILE)", SYS | FATAL);
    
       while((ch = fgetc(fp1)) != EOF)
          if(ch == '\n')
             countA++;
       fclose(fp1);
    
       while((ch = fgetc(fp2)) != EOF)
          if(ch == '\n')
             countB++;
       fclose(fp2);
    
       diff = countA - countB;
    I have tried this and the results are the same as with the test, however, I do not know enough of the consequences to be able to look further into it !


    tia,

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    98
    If I may, Dave is just pointing out that because you checked that the files had been opened correctly when you opened them, there is no need to check them again before you use them, and also that you have tried to close each file twice. There is no 'harm' in either, but it would be neater to avoid duplication.
    I should have mentioned this n my original reply, but thought it best to concentrate on demonstrating the fgetc method for counting lines...
    There is no such thing as a humble opinion

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    93
    ahh - thank you both

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting groups of lines
    By Ayreon in forum C Programming
    Replies: 26
    Last Post: 03-04-2009, 09:51 AM
  2. counting number of lines in my output file
    By zesty in forum C Programming
    Replies: 34
    Last Post: 12-25-2007, 08:15 PM
  3. Counting the number of lines in a text file - help
    By Erkan in forum C Programming
    Replies: 11
    Last Post: 11-12-2005, 04:12 PM
  4. Beginner problem with counting lines in a file
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-25-2005, 05:47 PM
  5. Counting Number of Lines of file
    By dapernia in forum C Programming
    Replies: 1
    Last Post: 09-05-2003, 02:22 PM

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