C:将 char 转换为 float

标签 c

我想用 C 语言将 char 转换为 float。 但 atof() 和 strtof() 不起作用。

这是一些代码,也许有人可以帮助我! 困难在于我的号码包含 e-02

char temp = "6.345e-02"; // the original value is read from a file
float result;

result = atof(temp); //not working correct
result = strtof(temp, NULL); //also not working correct

编辑:新代码

FILE *file = fopen("file.txt", "r");
char c;
char *temp[11];
float result;
while((c=fgetc(file))!=EOF){
  if(c=='\t'){
    result = atof(temp); //here is the converting problem
    printf("%.6f\n", result);
  } else {
    temp[i] = c;
  }
}

最佳答案

您将 temp 声明为单个字符,但事实并非如此。 它是一个字符数组,因此必须将其声明为 char *temp

关于C:将 char 转换为 float,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55103955/

相关文章:

c - 缺少应该存在的符号

c - 理解 main 的不常见参数

c++ - 预取大量引用数据的实际限制

c - 在 C 中减去任意大整数

c - 联盟 : strange behavior [c]

mysql - C mysql_num_rows() 段错误

c - 定义移位操作的临时结果如何存储是C标准吗?

java - 如何在用户计算机上启动本地端口(编辑问题)

c++ - 包括来自 C++ 的 C-DLL

c - [] 优先于 * 运算符