2015-06-17 66 views
0

我已經試過這樣:轉換字符數組爲雙

#include <stdio.h> 
#include <stdlib.h> 

int main(int argc, char *argv[]) { 

FILE *txt1; 
char tam[13]; 
char tam1[13]; 
char tam2[13]; 

txt1 = fopen("C:/Users/Hugo/Documents/C/in2.txt","r"); 
if(txt1 == NULL) 
{ 
    printf("No se puede leer el archivo"); 
    return 0; 
} 
fgets(tam1,sizeof(tam1), txt1); 
double c = atof(tam1); 
printf("%f",c); 

當我調試:tam1是 「3.1415926535」 但它打印此:

0.000000

+0

難道你的意思是'printf'' tam1',或'c'? – javabrett

+0

對不起,我的意思是c –

+0

試試'fscanf(txt1,「%lf」,&c)'。 – nneonneo

回答

0
#include <stdio.h> 
#include <stdlib.h> 
int main(int argc, char *argv[]) 
{ 
     FILE *txt1; 
     char tam[13]; 
     char tam1[13]; 
     char tam2[13]; 
     txt1 = fopen("99.c","r"); 
     if(txt1 == NULL) 
     { 
       printf("No se puede leer el archivo"); 
       return 0; 
     } 
     fgets(tam1,sizeof(tam1), txt1); 
     double c = atof(tam1); 
     printf("%f",c);// change from c 
} 

o/p: 
[email protected]:~/rabi/c$ ./a.out 
[email protected]:~/rabi/c$ vi 4.c 
+0

它仍然不起作用 –

+0

對我來說,它的工作正常o/p是預期的。 –

+0

現在我的問題是我需要得到0.00000056,但它將其轉換爲5.6 –