Thread: Char * to Float

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    14

    Char * to Float

    Hi,

    I'm programming C in visual studio 2008.

    What is the best way to convert/cast from a char* to a float value. atof does not seem to be working. :/

    Regards.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by kovitch View Post
    Hi,

    I'm programming C in visual studio 2008.

    What is the best way to convert/cast from a char* to a float value. atof does not seem to be working. :/

    Regards.
    Post your code... lets see what's going on...

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    14
    Code:
    void carrega_tabela(const char * ficheiro, int deficiente, int dependentes){
    
    	int i, j, marcador = 0;
    	float aux;
    	float taxa_irs;
    
    	float tabela[37][6];
    
    	FILE *f;
    
    	char * procura;
    	char string[1000], seps[] = " \n";
    	char *p;
    
    	if (deficiente == 0){
    		switch(dependentes){
    			case 0: procura = "NCASADO\0"; break;
    			case 1: procura = "CASADOUM\0"; break;
    			case 2: procura = "CASADODOIS\0"; break;
    			default: printf("<Erro: Numero de titulares incorrecto>"); break;
    		}
    	}else{
    		switch(dependentes){
    			case 0: procura = "NCASADODEF\0"; break;
    			case 1: procura = "CASADOUMDEF\0"; break;
    			case 2: procura = "CASADODOISDEF\0"; break;
    			default: printf("<Erro: Numero de titulares incorrecto>"); break;
    		}
    	}
    
    	printf("Procurar: %s\n", procura);
    
    	f=fopen(ficheiro,"r");
    	if(!f)
    		return 1;
    
    	i=0;
    
    	while(fgets( string, sizeof(string)-1, f) != NULL)
    	{
    		/* Break into tokens. */
    		p = string;
    		p = strtok( string, seps ); /* Find first token*/
    
    		j=0;
    	
    		while( p != NULL )
    		{
    			if (marcador){
    				aux = atof(p);
    				printf("String: %s ", p);
    				printf("-> aux: = %.1lf - j=%d - i=%d\n",aux,j,i);
    				tabela[i][j] = aux;
    				j++;
    			}
    
    			
    			if (strcmp(procura,p)==0){
    				marcador = 1;
    			}
    			p = strtok( NULL, seps ); /* Find next token*/
    		}
    		i++;
    	}
    
    	/*for (i=0; i < 6; i++){ 
    		for (j=0; j < 37; j++ ){
    			printf(" %.2lf",tabela[i][j]);
    		}
    		printf("\n");
    	}*/
    }
    The source file is:

    Code:
    NCASADO
    575.00 0.0 0.0 0.0 0.0 0.0 0.0
    580.00 1.0 0.0 0.0 0.0 0.0 0.0
    587.00 2.0 0.0 0.0 0.0 0.0 0.0
    633.00 3.0 1.0 0.0 0.0 0.0 0.0
    675.00 4.0 2.0 1.0 0.0 0.0 0.0
    726.00 5.0 3.0 2.0 1.0 0.0 0.0
    801.00 6.0 5.0 3.0 2.0 1.0 0.0
    907.00 7.0 6.0 4.0 3.0 2.0 1.0
    988.00 8.0 7.0 6.0 4.0 3.0 2.0
    1048.00 9.0 8.0 7.0 6.0 4.0 3.0
    1124.00 10.0 9.0 8.0 7.0 6.0 5.0
    1205.00 11.0 10.0 9.0 8.0 7.0 6.0
    1300.00 12.0 11.0 10.0 9.0 8.0 7.0
    1401.00 13.0 12.0 11.0 10.0 10.0 9.0
    1537.00 14.0 13.0 12.0 12.0 11.0 10.0
    1683.00 15.5 14.5 14.5 13.5 12.5 11.5
    1840.00 16.5 15.5 15.5 14.5 13.5 13.5
    1945.00 17.5 16.5 16.5 15.5 15.5 14.5
    2056.00 18.5 17.5 17.5 16.5 16.5 15.5
    2182.00 19.5 18.5 18.5 17.5 17.5 16.5
    2328.00 20.5 19.5 19.5 18.5 18.5 17.5
    2495.00 21.5 21.5 20.5 20.5 19.5 19.5
    2722.00 22.5 22.5 21.5 21.5 20.5 20.5
    3054.00 23.5 23.5 22.5 22.5 21.5 21.5  
    3478.00 24.5 24.5 23.5 23.5 23.5 22.5           
    4052.00 25.5 25.5 24.5 24.5 24.5 24.5
    4576.00 27.0 26.5 25.5 25.5 25.5 25.5
    5111.00 28.0 27.5 27.5 26.5 26.5 26.5
    5786.00 29.0 28.5 28.5 27.5 27.5 27.5
    6653.00 30.5 29.5 29.5 28.5 28.5 28.5
    7852.00 31.5 30.5 30.5 30.5 29.5 29.5
    9455.00 33.0 32.0 32.0 32.0 32.0 31.0
    11159.00 34.0 33.0 33.0 33.0 33.0 32.0
    18648.00 35.0 34.0 34.0 34.0 34.0 33.0
    20000.00 36.0 35.0 35.0 35.0 35.0 34.0
    22500.00 36.5 36.0 36.0 36.0 36.0 35.0
    25000.00 37.0 37.0 37.0 37.0 37.0 36.0
    25000.00 38.0 38.0 38.0 38.0 38.0 37.0
    Can it be because of the dot instead of the comma?
    Last edited by kovitch; 06-02-2011 at 06:37 AM.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You have a very consistent line format there... 7 floats per line... In this case I would load it into a string using fgets() as you are doing now and then use sscanf() to extract the data... That would reduce the entire while loop, reading the file, to about 3 lines of code.

    You should be able to look up sscanf() in your compiler's library documentation.

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Convert and cast have different meaning.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    float tabela[37][6];
    Your rows have 7 values each but each row of the array only has room for up to 6 columns worth of data.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert from float to char*
    By swanley007 in forum C++ Programming
    Replies: 4
    Last Post: 02-22-2006, 01:58 AM
  2. convert char to float
    By hdragon in forum C++ Programming
    Replies: 6
    Last Post: 10-23-2005, 09:43 PM
  3. int char and float
    By makveli in forum C++ Programming
    Replies: 6
    Last Post: 11-10-2003, 03:32 PM
  4. Char array to a float?
    By Nippashish in forum C++ Programming
    Replies: 8
    Last Post: 11-03-2002, 09:01 PM
  5. Float to char *
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 05-12-2002, 08:11 AM