我想用 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/