c语言文件中存入/读取double型矩阵型数据
/* * test.c * * Created on: Jun 1, 2019 * Author: lgh */ #include <stdio.h> #include <stdlib.h> double** allocation_memory_double(int row, int cow) { int i = 0; double** p; p = (double**)malloc(row*sizeof(double*)); for (i = 0; i<row; i++) { p[i] = (double*)malloc(cow*sizeof(double)); } return p; } void* free_memory_double(double** p, int row) { if (NULL==p) { return NULL; } int i = 0; for (i = 0; i<row; i++) { free(p[i]); } free(p); return NULL; } int main(int argc, char* argv[]) { double **M = allocation_memory_double(12,12); double **N = allocation_memory_double(12,12); int i,j; for(i=0;i<12;i++) { for(j=0;j<12;j++) { M[i][j] = i*12+j; } } FILE *file; if((file=fopen("/home/lgh/Desktop/aaa.txt","wb"))==NULL) { perror("无法打开该文件.\n"); return -1; } for(i=0;i<12;i++) { for(j=0;j<12;j++) { fprintf(file, "%11f", M[i][j]); } fprintf(file,"\n"); } fclose(file); double a[144]={0}; FILE *fpRead=fopen("/home/lgh/Desktop/aaa.txt","r+"); if(fpRead==NULL) { return -1; } for(i=0;i<144;i++) { fscanf(fpRead,"%lf ",&a[i]); } fclose(fpRead); for(i=0;i<12;i++) { for(j=0;j<12;j++) { N[i][j] = a[i*12+j]; printf("N[%d][%d]=%10f ",i,j, N[i][j]); } printf("\n"); } free_memory_double(M,12); free_memory_double(N,12); return 0; }
写入后的文件数据:
打印信息:
N[0][0]= 0.000000 N[0][1]= 1.000000 N[0][2]= 2.000000 N[0][3]= 3.000000 N[0][4]= 4.000000 N[0][5]= 5.000000 N[0][6]= 6.000000 N[0][7]= 7.000000 N[0][8]= 8.000000 N[0][9]= 9.000000 N[0][10]= 10.000000 N[0][11]= 11.000000 N[1][0]= 12.000000 N[1][1]= 13.000000 N[1][2]= 14.000000 N[1][3]= 15.000000 N[1][4]= 16.000000 N[1][5]= 17.000000 N[1][6]= 18.000000 N[1][7]= 19.000000 N[1][8]= 20.000000 N[1][9]= 21.000000 N[1][10]= 22.000000 N[1][11]= 23.000000 N[2][0]= 24.000000 N[2][1]= 25.000000 N[2][2]= 26.000000 N[2][3]= 27.000000 N[2][4]= 28.000000 N[2][5]= 29.000000 N[2][6]= 30.000000 N[2][7]= 31.000000 N[2][8]= 32.000000 N[2][9]= 33.000000 N[2][10]= 34.000000 N[2][11]= 35.000000 N[3][0]= 36.000000 N[3][1]= 37.000000 N[3][2]= 38.000000 N[3][3]= 39.000000 N[3][4]= 40.000000 N[3][5]= 41.000000 N[3][6]= 42.000000 N[3][7]= 43.000000 N[3][8]= 44.000000 N[3][9]= 45.000000 N[3][10]= 46.000000 N[3][11]= 47.000000 N[4][0]= 48.000000 N[4][1]= 49.000000 N[4][2]= 50.000000 N[4][3]= 51.000000 N[4][4]= 52.000000 N[4][5]= 53.000000 N[4][6]= 54.000000 N[4][7]= 55.000000 N[4][8]= 56.000000 N[4][9]= 57.000000 N[4][10]= 58.000000 N[4][11]= 59.000000 N[5][0]= 60.000000 N[5][1]= 61.000000 N[5][2]= 62.000000 N[5][3]= 63.000000 N[5][4]= 64.000000 N[5][5]= 65.000000 N[5][6]= 66.000000 N[5][7]= 67.000000 N[5][8]= 68.000000 N[5][9]= 69.000000 N[5][10]= 70.000000 N[5][11]= 71.000000 N[6][0]= 72.000000 N[6][1]= 73.000000 N[6][2]= 74.000000 N[6][3]= 75.000000 N[6][4]= 76.000000 N[6][5]= 77.000000 N[6][6]= 78.000000 N[6][7]= 79.000000 N[6][8]= 80.000000 N[6][9]= 81.000000 N[6][10]= 82.000000 N[6][11]= 83.000000 N[7][0]= 84.000000 N[7][1]= 85.000000 N[7][2]= 86.000000 N[7][3]= 87.000000 N[7][4]= 88.000000 N[7][5]= 89.000000 N[7][6]= 90.000000 N[7][7]= 91.000000 N[7][8]= 92.000000 N[7][9]= 93.000000 N[7][10]= 94.000000 N[7][11]= 95.000000 N[8][0]= 96.000000 N[8][1]= 97.000000 N[8][2]= 98.000000 N[8][3]= 99.000000 N[8][4]=100.000000 N[8][5]=101.000000 N[8][6]=102.000000 N[8][7]=103.000000 N[8][8]=104.000000 N[8][9]=105.000000 N[8][10]=106.000000 N[8][11]=107.000000 N[9][0]=108.000000 N[9][1]=109.000000 N[9][2]=110.000000 N[9][3]=111.000000 N[9][4]=112.000000 N[9][5]=113.000000 N[9][6]=114.000000 N[9][7]=115.000000 N[9][8]=116.000000 N[9][9]=117.000000 N[9][10]=118.000000 N[9][11]=119.000000 N[10][0]=120.000000 N[10][1]=121.000000 N[10][2]=122.000000 N[10][3]=123.000000 N[10][4]=124.000000 N[10][5]=125.000000 N[10][6]=126.000000 N[10][7]=127.000000 N[10][8]=128.000000 N[10][9]=129.000000 N[10][10]=130.000000 N[10][11]=131.000000 N[11][0]=132.000000 N[11][1]=133.000000 N[11][2]=134.000000 N[11][3]=135.000000 N[11][4]=136.000000 N[11][5]=137.000000 N[11][6]=138.000000 N[11][7]=139.000000 N[11][8]=140.000000 N[11][9]=141.000000 N[11][10]=142.000000 N[11][11]=143.000000