将float数字按照一定格式写入到文件中

/*
float.c
*/
#include<stdio.h>
#include<string.h>

int main()
{

    FILE *fp = fopen("Data.txt","w+");
    float f[3][5] = {{-3.41, 5.63, 6, 7.8, 1.3},
        {4.5 , -83, -23.5, 234, -23.794},
        {-12.5, -96.2, -8347.2, 135, -629.0},
    };

    char buf[64];
    int offset = 0;
    int i,j;
    for (j = 0 ; j < 3; j++)
    {
        offset = 0;
        for (i = 0 ; i < 5; i++)
        {
            offset += sprintf(buf+offset, "%.2f ", f[j][i]);
        }
        sprintf(buf + offset,"
");
        printf("%s",buf);
        fwrite(buf, strlen(buf), 1, fp);
    }
    fclose(fp);
    return 0;
}
原文地址:https://www.cnblogs.com/wanghao-boke/p/11933169.html