大量三维数据导入到C语言三维数组中的解决办法(第三次发帖,求版主勿喷)
运用动态数组的建立对大量数据进行导入并计算,下面是程序代码:程序代码:
#include <stdlib.h> #include <stdio.h> float main() { int n1,n2,n3; float ***array; int i,j,k; FILE* fp=fopen("all.txt","r"); if(fp==NULL) { printf("No files"); return -1; } puts("put the length of the first dimensional:"); scanf("%d",&n1); puts("put the length of the second dimensional:"); scanf("%d",&n2); puts("put the length of third dimensional:"); scanf("%d",&n3); array=(float***)malloc(n1*sizeof(float**));//the first dimension for(i=0; i<n1; i++) { array[i]=(float**)malloc(n2*sizeof(float*)); //the second dimension for(j=0;j<n2;j++) { array[i][j]=(float*)malloc(n3*sizeof(float)); //the third dimension for(k=0;k<n3;k++) { fscanf(fp,"%f",&array[i][j][k]); } } } for(i=0;i<n1;i++) { for(j=0;j<n2;j++) { for(k=0;k<n3;k++) { printf("%f ",array[i][j][k]); } printf("\n"); } } /***************************************************/ for(i=0;i<n1;i++) { for(j=0;j<n2;j++) { free(array[i][j]);//free the pointer of the third dimension } } for(i=0;i<n1;i++) { free(array[i]);//free the pointer of the second dimension } free(array);//free the pointer of the first dimension return 0; }
可能不入一些高手的眼,不过希望对和我一样的C语言菜鸟们有一些用处,也希望高手们能指教一二,谢谢