关于文件使用的问题,如何解决?

为什么运行就显示0.00?

#include<stdio.h>
int main()
{
    FILE*fp;
    float a,b,c,sum;
        fp=fopen("in.txt","rt+");
    fscanf(fp,"%f,%f,%f",&a,&b,&c);
    sum=a*0.1+b*0.2+c*0.7;
    fp=fopen("out.txt","wt+");
    fprintf(fp,"%4.2f",sum);
    fclose(fp);
    printf("%4.2f",sum);
    return 0;
}

这是 fscanf……是从文件输入流读取,不是控制台输入流。不是要你输入的,是从文件读取。

你得看看a,b,c都读出来是啥值啊。别人也不知道你文件里存储了啥,咋解决

为什么在DEV-c++里面一运行就显示0.00,无法输入a,b,c

修改如下,供参考:

#include<stdio.h>
int main()
{
    FILE* fp;
    float a, b, c, sum;
    char  str[128];
    fp = fopen("in.txt", "rt+");
    if (fp) {
        fscanf(fp, "%s", str); //fscanf(fp, "%f,%f,%f", &a, &b, &c);
        fclose(fp);
        sscanf(str, "%f,%f,%f", &a, &b, &c);
        sum = a * 0.1 + b * 0.2 + c * 0.7;
        fp = fopen("out.txt", "wt+");
        fprintf(fp, "%4.2f", sum);
        fclose(fp);
        printf("%4.2f", sum);
    }
    else
        printf("open file fail!");
    return 0;
}