Thread: Reading the number of floating point numbers in an array

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    3

    Reading the number of floating point numbers in an array

    How can I read the number of floating point numbers out of a string?

    I have looked at a number of functions, but none seem to be what i need.

    So far I have:

    Code:
    while(fgets(temp,2047,FilePointer) != NULL)
    {
            //read number of floating points
    }
    temp is a char array and FilePointer is a pointer to the input file.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    There's no single, magic function for that, but you could use a loop along with sscanf or strtod to see how many you count.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    3
    Thanks! I am trying the following code,

    Code:
    while(fgets(temp,2047,FilePointer) != NULL)
    {
    
         a++;
         char *ptr = temp;
    
         while(sscanf(ptr,"%lf%n",word,&n) == 1)
         {
              b++;
              //printf("%d\n",b);
              ptr += n;
         }
    
    }
    However I can't make sense of the output. The loop seems to run without any real bearing on the size of temp.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Without knowing what word is, it's really hard to say, though I'm guessing word isn't actually a double, as your sscanf call would suggest. Also, you may have problems with the %n modifier:
    Quote Originally Posted by man scanf
    n Nothing is expected; instead, the number of characters consumed thus
    far from the input is stored through the next pointer, which must be a
    pointer to int. This is not a conversion, although it can be
    suppressed with the * assignment-suppression character. The C standard
    says: "Execution of a %n directive does not increment the assignment
    count returned at the completion of execution" but the Corrigendum
    seems to contradict this. Probably it is wise not to make any
    assumptions on the effect of %n conversions on the return value.
    strtod has better error handling and will store the address in your string of where the conversion left off (i.e. where the float/double stopped). From that pointer, you can then keep stepping until you hit another digit (skipping spaces, alpha and most punctuation), and try another strtod.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    word is a very strange way of writing 'pointer to double'
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    3
    I apologise for posting so late after receiving many useful replies. Thank you for your help.

    I am working on a remote server so was not able to copy and paste code when i previosly posted, hopefully this will give you a better idea of what i was trying to acomplish.

    The code appears to work for what i can throw at it, however I'm not sure i fully understood the use of pointers in the function sscanf. I am also concerned about declaring an array with it's size as a variable without using alloc, even though this seems to work in this case.

    The general idea was to get a matrix into memory from a file, without using any non standard libraries.

    Feel free to rip this to shreds with constructive criticism:

    Code:
    #include<stdio.h>
    #include<math.h>
    
    int main(int argc, char *argv[])
    {
    	int a=0, b=0, checkb1=0, checkb2=0, x=0, n=0, MatrixHeight=0,MatrixWidth=0	;
    	char temp[2048] , word[100];
    	char *ptr;
    	FILE *FilePointer;
    
    
    	if (NULL==(FilePointer=fopen(argv[1],"r")))	//open and check file
    	{
    		printf("Inccorect Usage\n\n");
    		return -1;
    	}
    	
    	while(fgets(temp,2047,FilePointer) != NULL)	//while not the end of the file
    	{
    		if(sscanf(temp,"%s",word) < 1) 		//make sure there's something in the line, before adding a
    		goto end;
    		a++;
    		b=0;					//reset b for a new line
    		ptr = temp;
    		while(sscanf(ptr,"%lf%n",word,&n) == 1)
    		{
    			b++;
    			ptr += n;
    		}
    		if((checkb1!=0)&&(checkb2!=b))		//make sure the width is always the same
    		{
    			printf("Matrix is missing elements!\n\n");
    			return -1;
    		}
    		checkb1++;
    		checkb2=b;
    		end:;
    	}
    	
    	printf("Matrix is a %d by %d matrix\n", a, b );
    
    	double MatrixSize[a][b];			//get the matrix into memory
    	rewind(FilePointer);
    	MatrixHeight = a;
    	MatrixWidth = b;
    	a = 0;
    	b = 0;
    	for ( ; a < MatrixHeight ; a++)
    	{
    		b=0;
    		for ( ; b < MatrixWidth ; b++)
    		fscanf(FilePointer,"%lf",&MatrixSize[a][b]);
    	}
    	a=0;
    	for ( ; a < MatrixHeight ; a++)
    	{
    		b=0;
    		printf("\n");
    		for ( ; b < MatrixWidth ; b++)
    		printf("%3lf\t",MatrixSize[a][b]);
    	}
    	printf("\n");
    	return 0;
    }

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by mfurseman View Post
    The code appears to work for what i can throw at it, however I'm not sure i fully understood the use of pointers in the function sscanf.
    Nope, you don't quite get it (you're having some trouble with printf too). The format strings for scanf and printf and all subsequent parameters must have corresponding types. If you want to print a string using printf, you use "%s", then you have to pass it a char *, "%d" in printf requires an int. In scanf, "%s" is for reading strings, so it needs a char * pointing to a place to store that text. "%lf" is for a double, so it needs the address of a double variable to store info. That makes this line invalid:
    Code:
    while(sscanf(ptr,"%lf%n",word,&n) == 1)
    You need to replace 'word' with the address of a double, or you need to change "%lf" to "%s", depending on what you want to read in.


    I am also concerned about declaring an array with it's size as a variable without using alloc, even though this seems to work in this case.
    Variable length arrays are legal in C99, so you're fine there as long as you use a C99 compliant compiler.

    The only other major problem I noticed is your use of goto. It should be avoided whenever possible. You could replace that goto by reversing your if statement:
    Code:
    if(sscanf(temp,"%s",word) >= 1) 		//make sure there's something in the line, before adding a
    {
        a++;
        b=0;					//reset b for a new line
        ptr = temp;
        while(sscanf(ptr,"%lf%n",word,&n) == 1)
        {
            b++;
            ptr += n;
        }
        if((checkb1!=0)&&(checkb2!=b))		//make sure the width is always the same
        {
            printf("Matrix is missing elements!\n\n");
            return -1;
        }
        checkb1++;
        checkb2=b;
    }
    Oh, one more thing. You should put the a = 0 and b = 0 in the initialization of the for loops instead of above them:
    Code:
    for (a = 0; a < MatrixHeight; a++)
    It makes your code more readable.
    Last edited by anduril462; 03-16-2011 at 11:55 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 06-28-2010, 03:13 PM
  2. Random number array
    By matt_570 in forum C++ Programming
    Replies: 12
    Last Post: 11-13-2008, 04:44 PM
  3. floating point array
    By luigi40 in forum C# Programming
    Replies: 2
    Last Post: 12-09-2005, 04:37 PM
  4. Floating point numbers
    By bulsquare in forum C Programming
    Replies: 2
    Last Post: 04-10-2002, 04:44 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

Tags for this Thread