Thread: Counting groups of lines

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by matsp View Post
    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
    Then that explains the bit in parentheses -- I thought it was an assignment, but instead a replacement.

    Well, then. I'm guessing (sheer guess) that your complaint about only seeing two lines at the top of the screen is just because the rest have scrolled off, since you're doing this 84 times.

    And so, how are the numbers off? Do you have lines longer than 100 characters, as mats mentioned earlier?

  2. #17
    Astrophysics student Ayreon's Avatar
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    79
    I'm sorry for the confusion. My program may be a little messy, I sometimes use english variable names and sometimes dutch, no particular reason, it just happens. I'll post them in english from now on to avoid confusion .

    This is pretty much the whole program:

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <math.h>
    
    long no_lines=0;
    char *dummyline;
    
    char *Combinations_systemname[84] = {"Black Hole - Carbon Star","Black Hole - Helium Star","Black Hole - Black Hole", and a whole lot more combinations.......};
    
    
    //********************
    //Functions:
    //********************
    
    int Linecount(char* filename_array[],int n){
    
         char filename[100]; 
         no_lines=0;
         sprintf(filename, "%s.txt", filename_array[n]);
    
    
    
         FILE* file = fopen(filename,"r");
         
         
         if (file!=NULL){
         
    
             char dummyline[1000];
          
             while ( fgets(dummyline, 1000, 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
    
             fclose(file); 
    
             return no_lines;
         
             
         
         } else {printf("!! %s File not available\n",filename);
                 return no_lines=0;
                }
    
          
    
    }
    
    //********************
    //Main Program
    //********************
    
    
    int main (){
    
    int no_of_systems=0;
    
       
    FILE* No_binarysystems_file = fopen("Aantal_dubbelsterren.txt","w");
    
      
    
    int i=0;
    
    
    
        for (i=0;i<84;i++){
                   
                     Linecount(Combinations_systemname,i);
                     
    
                     
                     no_of_systems = no_lines/3;
                     
                     printf("no_lines=%d \n",no_lines);
                     
                     printf("Number of systems of this type: %d\n\n",no_of_systems);
                     
                     fprintf(No_binarysystems_file,"%s,%d\n",Combinations_systemname[i],no_of_systems);  
    }
    
    
             
    
    fclose(No_binarysystems_file);
    
    //================================================//   
    
      //to make sure window doesn't close right away.
       #ifdef _WIN32
         system("PAUSE");
       #endif
       
    
    
    
        
       return (0);
    }
    Meanwhile I've found a corrupt textfile and that one happend to be the one I tested if the number of systems was correct. Not much of a test! Anyway. It does seem to work afterall.
    Last edited by Ayreon; 03-03-2009 at 02:46 PM. Reason: removed unnessecary question

  3. #18
    Astrophysics student Ayreon's Avatar
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    79
    Quote Originally Posted by tabstop View Post
    Well, then. I'm guessing (sheer guess) that your complaint about only seeing two lines at the top of the screen is just because the rest have scrolled off, since you're doing this 84 times.

    While typing my reply this came in. I guess that explains it.

    I think I'm done then after all.

    Thanks.

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    no_lines has no reason to be a global, so you should fix that up.

    No reason for a global variable char * dummyline.

    Code:
    char *Combinations_systemname[84] = {"Black Hole - Carbon Star","Black Hole - Helium Star","Black Hole - Black Hole", and a whole lot more combinations.......};
    Why not use a line per each, like this:
    Code:
    char *Combinations_systemname[] = 
    {
         "Black Hole - Carbon Star",
         "Black Hole - Helium Star",
         "Black Hole - Black Hole", 
         and a whole lot more combinations.......
    };
    To make it simple to change, I also removed the 84 in the brackets. Of course, you will have to change the for-loop:

    Code:
        for (i=0;i<sizeof(Combinations_systemname)/sizeof(Combinations_systemname[0]);i++)
    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.

  5. #20
    Astrophysics student Ayreon's Avatar
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    79
    But now in order to make it work I need to declare no_lines twice. In the function itself and in the main.

  6. #21
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Ayreon View Post
    But now in order to make it work I need to declare no_lines twice. In the function itself and in the main.
    Yes, but it's much "better" that way - the reason is that you have control over where each of those no_lines variables are being used. Global variables tend to get messy after a while, and there's no good way (once the program gets a bit larger) to track where the variables are updated and where it is safe to change it. It is also quite likely that the compiler will generate better code for local variables.

    --
    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.

  7. #22
    Astrophysics student Ayreon's Avatar
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    79
    I also don't understand this part of the for loop:
    sizeof(Combinations_systemname)/sizeof(Combinations_systemname[0])

    Why can't I just use "sizeof(Combinations_systemname)". What is the other part for?

  8. #23
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Ayreon View Post
    I also don't understand this part of the for loop:
    sizeof(Combinations_systemname)/sizeof(Combinations_systemname[0])

    Why can't I just use "sizeof(Combinations_systemname)". What is the other part for?
    The first sizeof gives the number of bytes occupied by the char * in the array - since that is 4 or 8 times larger than what you want, we divide by the first element (which is a single char *), and get the number of elements in the array - it s a "standard way" of counting the number of things in an array. Just remember that this ONLY works if you have the original array - it doesn't work for arrays passed into a function.

    Oh, and change char * to const char * in the declaration of Combinations_systemname - that prevents someone from accidentally changing the code to write to the array and causing a crash (because the elements can not be written to).
    --
    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. #24
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Ayreon View Post
    I also don't understand this part of the for loop:
    sizeof(Combinations_systemname)/sizeof(Combinations_systemname[0])

    Why can't I just use "sizeof(Combinations_systemname)". What is the other part for?
    Most likely, using your n=84 example, sizeof(Combinations_systemname) is 336 (since each char* is probably 4 bytes -- if it was eight, double it).

  10. #25
    Astrophysics student Ayreon's Avatar
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    79
    Ok, that's clear. Thanks for the extra "tidying up".
    If you got any more tips, they're welcome.

    Oh, and one more thing, what exactly do you mean by "passed into a function". Isn't that what happens with my array, I am using it in my function. "Passed into function" probably isn't the same as "used by function" then?

  11. #26
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Ayreon View Post
    Ok, that's clear. Thanks for the extra "tidying up".
    If you got any more tips, they're welcome.

    Oh, and one more thing, what exactly do you mean by "passed into a function". Isn't that what happens with my array, I am using it in my function. "Passed into function" probably isn't the same as "used by function" then?
    By passed to a function, I mean just that - an argument to a function. Not "used in a function". Since it is a global variable (and this sort of global is OK - it is not modified, for one thing).

    --
    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. #27
    Astrophysics student Ayreon's Avatar
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    79
    Earlier on in this topic Matsp said:

    Quote Originally Posted by matsp View Post
    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);
    I'm trying to combine several programs into one, and I want to improve the reliability. So I think it's a good idea to use this in case a line is too long (I did find such lines in two corrupt files).

    Am I doing it right this way?:
    Code:
    int len;
    
    while ( fgets(dummyline, 1000, file) != NULL) {
                  len = strlen(dummyline);
                  if (len && dummyline(len-1) != '\n'){
                           printf("dummyline is not big enough at %d lines\n", no_lines);
                  }else{
                           no_lines++;
                           }
    }
    Or maybe in stead of printf I could save a variable with this information, because de printf screen can only hold so many lines and this messege could fall off, and you would never notice it happened, right?
    Last edited by Ayreon; 03-04-2009 at 10:52 AM. Reason: typo
    Nothing to see here, move along...

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, 05:12 PM
  2. Beginner problem with counting lines in a file
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-25-2005, 06: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, 05:09 AM