Convert char array into double

1.6k views Asked by At

I have tried this:

#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);

when I debug: tam1 is "3.1415926535" But it prints this:

0.000000

1

There are 1 answers

4
rabi shaw On BEST ANSWER
#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:
rabi@rabi-VirtualBox:~/rabi/c$ ./a.out 
3.141593rabi@rabi-VirtualBox:~/rabi/c$ vi 4.c