Counting groups of lines

This is a discussion on Counting groups of lines within the C Programming forums, part of the General Programming Boards category; I need to count how many groups of 3 lines there are in a textfile. I wanted to simply do ...

  1. #1
    Astrophysics student Ayreon's Avatar
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    79

    Counting groups of lines

    I need to count how many groups of 3 lines there are in a textfile. I wanted to simply do this by counting the lines and deviding that by three. But sometimes there is an extra empty line at the end of the file.

    I need the number of groups (no_groups) to be an integer. And I nee to make sure it doesn't count a group too many.

    I've using this function, which call's to itself in case there are even two extra lines (I doubt there will be):

    Code:
    long no_linesdiv=0;
    long no_lines=0;
    
    
    int divisible(long number,int deelbaardoor){
        
        if (number%3 != 0){
        number--;
        divisible(number,deelbaardoor);
        }else{
              return no_linesdiv;
              }
        
    }
    
    and in the main:
    
    divisible(no_lines,3);
                     
                     no_groups = no_linesdiv/3;
    But now I keep getting 0 groups

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    In your recursive call, you need to return divisible(whatever) rather than just call the function.

    Edit: Also note that this is way way way way way way way way way way way way way way way way more complicated than it needs to be; an integer divided by an integer will always give you an integer result, so 16/3 will be 5.

  3. #3
    Astrophysics student Ayreon's Avatar
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    79
    Hmm, funny.
    Well it was a good practice thinking about this .
    Can i also devide a long integer by an integer without problems?

  4. #4
    Astrophysics student Ayreon's Avatar
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    79
    Can I make this work anyway? Because just to see if it worked i tried returning divisible(whatever), but I still get zero for the number of groups.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    int divisible(long number,int deelbaardoor){
        
        if (number%deelbaardoor != 0){
        number--;
        return divisible(number,deelbaardoor);
        }else{
              return number/deelbaardoor;
              }
        
    }

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Ayreon View Post
    Hmm, funny.
    Well it was a good practice thinking about this .
    Can i also devide a long integer by an integer without problems?
    An integral type (int, long, long long, char, or unsigned version thereof) divded by another integral type will always give an integer answer (of the largest type involved).

  7. #7
    Astrophysics student Ayreon's Avatar
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    79
    I still get strange numbers sometimes. I some cases It counts the correct number of groups, but in many cases I get numbers that are way too high. It doesn't seem to be very reliable for some reason. (Now I have simply used no_lines/3).

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, what is the total number of lines when you get "strange results"?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Astrophysics student Ayreon's Avatar
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    79
    I've just tested it a little more and the problem seems to be the number of lines. Sometimes it counts the right number and sometimes it doesn't, strange. I would expect it to either work or not work at all.

    This is my function for counting the lines:

    Code:
    int Linecount(char* filename_array[],int n){
    
         char filename[100]; 
    
         sprintf(filename, "%s.txt", filename_array[n]);
    
    
    
         FILE* file = fopen(filename,"r");
         
         
         if (file!=NULL){
         
             //dummyline = (char*) malloc (100);
             char dummyline[100];
          
             while ( fgets(dummyline, 100, file) != NULL) no_lines++;  
             
             printf("Scanned: %s\n",filename); //testing filename
             printf("%s",dummyline);  //just for testing if last line in the file is indeed from the correct file
    
             return no_lines;
              
             free (dummyline);  
         
             fclose(file); 
         
         } else {printf("!! %s Staat er niet bij\n",filename);
                 return no_lines=0;
                }
    
          
    
    }
    It reads the correct files, i could see that by showing the dummyline, which is the last line of the file (After its finished counting). So what's going wrong during the counting of some files??

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Why do you think no_lines is 0 when the function starts?

    And nothing happens after a return statement, so free(dummyline) will never happen. That's a good thing, since it will fail spectacularly (dummyline wasn't malloced). However that also means that the fclose won't happen either.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Is it possible that some lines are longer than 100 characters? How much wrong is it (is it "about right, but a few more" or "completely crazy numbers"?)

    If lines are longer than 100 characters (including the newline), then you will obviously miscount such lines as two lines. I'd be very tempted, if that is the case, to simply make the dummyline a lot larger (say 8192), so that really long lines can be read in. And to be sure, you may want to add:
    Code:
    int len;   // at the beginning of the while-loop. 
    ...
    // after fgets() != NULL
       len = strlen(dummyline);
       if (len && dummyline(len-1) != '\n')
          printf("dummyline is not big enough at %d lines\n", no_lines);
    Also, where do you set no_lines to zero? Why is it not a local variable in this function?

    Code:
             return no_lines;
              
             free (dummyline);  
         
             fclose(file);
    You probably should not leave the file open - if you do that many enough times, it will cause you not to be able to open any more files. How many files you can have open at any given time depends on the system, it can be a few (30 or so) or several thousand. But it is not unlimited...

    You should also not free dummyline as the code is right now?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Astrophysics student Ayreon's Avatar
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    79
    Ok I have deleted the free(dummyline), that makes sense, I had it because earlier I dit use malloc for the line.
    I also put the return at the end now.

    It does seem to work now, no more crazy numbers.

    One more thing though.
    Usually it prints 3 lines on screen, either these:
    Scanned: filename
    no_lines
    no_lines/3
    or these:
    !!File not present
    no_lines
    no_lines/3

    However the first two lines of the screen read:
    no_lines
    no_lines/3

    And after these it does everything its supposed to be doing.
    Why does it print these two lines?? They even have non-zero values.

    edit: I also put no_lines=0; ate the top of the function now, so it should always start with a zero value for no_lines.
    Last edited by Ayreon; 03-03-2009 at 12:49 PM.

  13. #13
    Astrophysics student Ayreon's Avatar
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    79
    I'm sorry I just found out the numbers aren't crazy, but they're not right either. I don't get it, it reads the right file, but it doesn't count the lines correctly.

    This is where I call my function:
    (aantal_regels=no_lines)

    Code:
    int i=0;
    
        for (i=0;i<84;i++){
                   
                     Linecount(Combinations_systemname,i);
                     
                     
                     
                     no_of_systems = aantal_regels/3;
                     
                     printf("aantal_regels=%d \n",aantal_regels);
                     
                     printf("aantal systemen van dit type: %d\n\n",no_of_systems);
                     
                     fprintf(Aantal_dubbelsterrenbestand,"%s,%d\n",Combinations_systemname[i],no_of_systems);  
    }
    Maybe this is getting a little off topic again?

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So you count the lines, and then you deliberately throw the answer away and divide some other completely unrelated number by 3. Why do you expect that to work? If you had done aantal_regels = Linecount(stuff) as your comment up top indicates then you might have something.

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by tabstop View Post
    So you count the lines, and then you deliberately throw the answer away and divide some other completely unrelated number by 3. Why do you expect that to work? If you had done aantal_regels = Linecount(stuff) as your comment up top indicates then you might have something.
    I suspect that aantal_regels is dutch (or German?) for no_lines, and that it is a global variable. It probably should be a local in both functions, and surely SHOULD be set to zero when entering the "count lines" function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 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
  2. Beginner problem with counting lines in a file
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-25-2005, 05:47 PM
  3. Counting Lines
    By darfader in forum C Programming
    Replies: 6
    Last Post: 09-12-2003, 06:19 AM
  4. Counting Number of Lines of file
    By dapernia in forum C Programming
    Replies: 1
    Last Post: 09-05-2003, 02:22 PM
  5. Counting Lines
    By drdroid in forum C++ Programming
    Replies: 7
    Last Post: 11-18-2002, 04:09 AM

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