如何将双精度数组写入文件?

时间:2016-01-11 09:13:33

标签: c

我使用以下代码将一个双精度数组写入.txt文件

double Wih[5][2];
FILE * qfile;
int    i;

qfile = fopen("E:/c.txt", "wb");    
for (i = 0; i < 10; i++) {
    fwrite(Wih[i], sizeof(double), 10, qfile);
}
fclose(qfile);

这是对的吗?

3 个答案:

答案 0 :(得分:1)

即使您知道有10个元素无法以[0] .. [9]的形式访问它们,但您需要使用这两个索引。另外,如果你正在进行10个循环打印10个元素,那就太多了!

double Wih[5][2];
FILE * qfile;
int    i,j;

qfile = fopen("E:/c.txt", "wb");    
for (i = 0; i < 5; i++) {
    for (j = 0; j < 2; j++) {
        fwrite(Wih[i][j], sizeof(double), 1, qfile);
    }
}
fclose(qfile);

另外,您应该检查fopen()fwrite()fclose()的返回值...所以让我们写一个完整的函数。

int writeWih ( void ) {

    double Wih[5][2];
    FILE * qfile;
    int    i,j;

    /* presumably some code which puts values in Wih */

    qfile = fopen("E:/c.txt", "wb");
    if (qfile == NULL) return -1;         /* error! */ 
    for (i = 0; i < 5; i++) {
        for (j = 0; j < 2; j++) {
            if (fwrite(Wih[i][j], sizeof(double), 1, qfile) != 1) {
                (void)fclose(qfile);
                return -1;                /* error */
            }
        }
    }
    return fclose(qfile);
}

顺便说一句,如果您希望将数据格式化为人类可读的格式,则应使用fprintf()代替fwrite()

答案 1 :(得分:0)

您在这里写了五个长度为2的双精度数组。更正了fwrite中的nitems参数和循环界限的上限。

double Wih[5][2];
FILE * qfile;
int    i;

qfile = fopen("E:/c.txt", "wb");    
for (i = 0; i < 5; i++) {
    fwrite(Wih[i], sizeof(double), 2, qfile);
}
fclose(qfile);

答案 2 :(得分:0)

任何问题都可以随意提问

#include <stdio.h>
#include <stdlib.h>

int saveArray(void *arr, int nsize, int cx, int cy, char *fname){
    FILE *fd;
    fd = fopen(fname,"wb");
    if(!fd) return 0;

    fwrite(&cx,sizeof(int),1,fd);
    fwrite(&cy,sizeof(int),1,fd);
    fwrite(arr,nsize,cx*cy,fd);

    fclose(fd);
    return 1;
}


int loadArray(void *arr, int nsize, int *pcx,int *pcy, char *fname){
    FILE *fd;
    if(! (pcx && pcy)){
        printf("error! pcx & pcy must be specified\n");
        return 0;
    }
    *pcx = *pcy = 0;  // initialize to zero.

    fd = fopen( fname, "rb");
    if( !fd ) return 0;


    fread(pcx, sizeof(int), 1, fd); // get saved cx
    fread(pcy, sizeof(int), 1, fd); // get saved cy

    // if array is specified then load it
    if(arr)
        fread(arr, nsize, (*pcx) * (*pcy) , fd);

    fclose(fd);
    return 1;
}

#define ARRAY_FILE    "c.txt"

// this function is to print out the content of the array (double*)
void printDblArray(double *arr, int cx , int cy){
    for(int x = 0 ; x < cx ; x++)
        for(int y = 0 ; y < cy ; y++)
            printf("arr[%d][%d] = %f\n", x, y, arr[ y + (x * cy ) ] );
}

int main(){
    int cx , cy;
    double arr[3][2] = {
        {1,1},
        {2,2},
        {3,3}
    }  ;


    printDblArray( (double*) arr , 3 , 2);

    // saving array in files
    saveArray(arr, sizeof(double) , 3 , 2 , ARRAY_FILE);


     printf("Data from file:\n");

    if(loadArray( NULL, sizeof(double), &cx, &cy, ARRAY_FILE)){ // we cary first the size of array
        double arr2[cx][cy]; // new variable arr2

        printf("cx = %d, cy = %d\n",cx , cy); 
        loadArray(arr2, sizeof(double), &cx, &cy, ARRAY_FILE); // then load the array

        printDblArray((double*)arr , cx , cy);
    }else{
        printf("error reading array\n");
    }

    return 0;
}