Thread: Read a well defined number of lines only

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    114

    Read a well defined number of lines only

    Hi everyone,

    I am trying to have a function that can read a file of floats.
    This is very straight forward and I did that with different types of files before; however, given that previously I knew exactly the information on the last line of the file, I never bothered in making the function distinguish between an empty (but still existing) last line, and an end of file.

    What I practically need to do is being able to count only the number of lines that indeed contain numbers, ruling out any additional blank line that may follow the set of numbers above.

    Ex.1) File with 3 lines of numbers and ending at 1 only empty line:
    Code:
    1 2
    3 4
    5 6
    Ex.2) File with 3 lines of numbers and an ARBITRARY sequence of blank lines following:
    Code:
    1 2
    3 4
    5 6
    blank
    ...
    ...
    blank
    I usually use the following code to read a file as in ex.1, but I am unsure on how to modify it in order to stop the counting at the last numbered line although there may be many more blank lines following:

    Code:
    if((file_id = fopen(file_name, "r")) == NULL){
    		printf(" ERROR, The file %s could not be open\n \n\n", file_name);
    		exit(1);
    	}
    	else{
    		rewind(file_id); //This line resets the pointer to the beginning of the file
    		
    		line_cntr = 0;
    		while(fgets(line, sizeof(line), file_id)!= NULL) {
    			
    			line_cntr++;
    			*nrows = line_cntr ;
    				
    		}//End WHILE
    	}
    	fclose(mesh_file_id);
    }
    Thank you in advance; I hope someone can give me a hint.

    Best
    Last edited by cfdprogrammer; 10-01-2009 at 06:24 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    while( fgets( foo, BUFSIZ, fp ) )
    {
        char *c = foo;
        while( c && *c && isspace( c ) ) c++;
        if( c && *c && !isspace( c ) )
        {
            ...do something with this line...
        }
    }
    Something like that should work.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Maybe something like this?

    Code:
    while((fgets(line), sizeof(line), file_id)!= NULL) { //
      if(line[0] =='\n')
        break;
      //continue with the rest of your processing
    }
    You could also test it using ctype.h header with any of several functions to confirm that line[0] is a number, is an alphanumeric, is not a space, etc.

    or
    Code:
    if(line[0] < '0')  //zero, not Oh
      break;
    The above would catch lines with blank spaces (which are 32 ascii), and newlines (ascii 10), but catch all numbers and letters, since 0 is 48 ascii.

    I believe I like the < '0' one better since it would be the more robust one to use.

    How about that?

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    114
    Hi Adak and Quasah,

    Thank you very much to both. I actually tried all the options, and although both options suggested by work fine, I could not get Quasah's to work.
    What happens with Quasah's solution is what would happen without using such solution and keeping the function as the original version.

    I really appreciate your help

    Thank you
    Best

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    114
    Hello Adak,

    Look, I have a similar issue to this posted earlier today and to which you helped me find a solution; now, instead of being able to recognize the additional blank lines at the end of the file, I want to be able to count correctly the number of entries within each row, independently on the size and type of "separator" between the entries.
    The code that I put below fails when, instead of 1 space between two consecutive numbers on the same line, there are 2 or more spaces, or when the line begins with a space instead of a number.
    I tried to see how I could apply your algorithm of above to this problem but I couldn't figure it out.

    I hope you can help with this as well,
    I appreciate.

    Ex. File 1: this is read correctly
    Code:
    1 2 3
    4 5 6
    6 7 8
    Ex. File 2: this is not read correctly:
    Code:
     1  2  3
     4  5  6
     5 6  7
       8  9 10
    Code:
    while(fgets(line, sizeof(line), mesh_file_id)!= NULL) {
    			line_cntr++;
    		
    			if(line_cntr > 0 && line_cntr <= nrows ){
    				ptr_conn = strtok(line, SEPARATOR);
    
    				cntr = 0;
    				while(ptr_conn != NULL){
    					ptr_conn = strtok(NULL, SEPARATOR);
    					cntr = cntr + 1;
    					*max_elnodes = cntr;
    				}
    
    				i++;
    			}
    		}
    	}//end of: if else

    Quote Originally Posted by Adak View Post
    Maybe something like this?

    Code:
    while((fgets(line), sizeof(line), file_id)!= NULL) { //
      if(line[0] =='\n')
        break;
      //continue with the rest of your processing
    }
    You could also test it using ctype.h header with any of several functions to confirm that line[0] is a number, is an alphanumeric, is not a space, etc.

    or
    Code:
    if(line[0] < '0')  //zero, not Oh
      break;
    The above would catch lines with blank spaces (which are 32 ascii), and newlines (ascii 10), but catch all numbers and letters, since 0 is 48 ascii.

    I believe I like the < '0' one better since it would be the more robust one to use.

    How about that?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    fgets + sscanf, check the return value of sscanf.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    114
    Quote Originally Posted by quzah View Post
    fgets + sscanf, check the return value of sscanf.


    Quzah.
    But can I count the number of entries by using sscanf? I dont actually have to store them to any array, but only count them for a subsequent call to another function

    thank you again

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by cfdprogrammer View Post
    But can I count the number of entries by using sscanf? I dont actually have to store them to any array, but only count them for a subsequent call to another function

    thank you again
    The return value of scanf is the number of item sucessfully read and stored:
    Code:
    scanf("%d %d",&x,&y);
    If two numbers are read in, the return value will be 2.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    114
    Thank you!

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your welcome. Next time, when I tell you something, try doing a tiny bit of research. You know, like looking up what a function actually does. If you had done that, you'd have known what I was talking about when I told you to check the return value.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Mar 2009
    Posts
    114
    Quote Originally Posted by quzah View Post
    Your welcome. Next time, when I tell you something, try doing a tiny bit of research. You know, like looking up what a function actually does. If you had done that, you'd have known what I was talking about when I told you to check the return value.


    Quzah.
    Hi Quzah,

    Sorry about that, you are right. Thanks fro helping.

    All the best,
    cfd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. count how many lines, allocate vector, and read again
    By patiobarbecue in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2009, 07:18 PM
  2. how to read a digit of a floating point number?????
    By spicy_centipede in forum C Programming
    Replies: 15
    Last Post: 07-14-2007, 11:43 AM
  3. Variables already defined while linking.
    By xconspirisist in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2005, 05:20 AM
  4. Header files
    By borland_man in forum C++ Programming
    Replies: 14
    Last Post: 02-22-2002, 04:30 AM
  5. Help! Can't read decimal number
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 09-07-2001, 02:09 AM