【问题标题】:Convert char array into double [closed]将char数组转换为double [关闭]
【发布时间】:2015-09-02 06:27:26
【问题描述】:

我试过这个:

#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 is "3.1415926535" 但它会打印:

0.000000

【问题讨论】:

  • 您的意思是printftam1,还是c
  • 改用fscanf(txt1, "%lf", &amp;c)
  • @nneonneo 没用
  • 我不知道出了什么问题。您的代码看起来基本正确,除非您的文件包含一些奇怪的字符或其他内容。
  • 是的,它是这样的......我修好了谢谢!

标签: c printf double fgets atof


【解决方案1】:
#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

【讨论】:

  • 还是不行
  • 对我来说,它的工作正常 o/p 符合预期。
  • 现在我的问题是我需要得到 0.00000056 但它将它转换为 5.6
  • 双 c = atof(tam1); printf("%.8f",c); o/p-->rabi@rabi-VirtualBox:~/rabi/c$ ./a.out 0.00000056rabi@rabi-VirtualBox:~/rabi/c$
猜你喜欢
  • 2013-11-28
  • 1970-01-01
  • 1970-01-01
  • 2013-03-13
  • 2011-11-19
  • 2011-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多