# Counting groups of lines

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 03-03-2009
Ayreon
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
• 03-03-2009
tabstop
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.
• 03-03-2009
Ayreon
Hmm, funny.
Can i also devide a long integer by an integer without problems?
• 03-03-2009
Ayreon
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.
• 03-03-2009
tabstop
Code:

```int divisible(long number,int deelbaardoor){         if (number%deelbaardoor != 0){     number--;     return divisible(number,deelbaardoor);     }else{           return number/deelbaardoor;           }     }```
• 03-03-2009
tabstop
Quote:

Originally Posted by Ayreon
Hmm, funny.
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).
• 03-03-2009
Ayreon
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).
• 03-03-2009
matsp
So, what is the total number of lines when you get "strange results"?

--
Mats
• 03-03-2009
Ayreon
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??
• 03-03-2009
tabstop
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.
• 03-03-2009
matsp
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
• 03-03-2009
Ayreon
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.
• 03-03-2009
Ayreon
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?
• 03-03-2009
tabstop
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.
• 03-03-2009
matsp
Quote:

Originally Posted by tabstop
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
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last