我试图分配一个多维数组从文件中读取信息,然后打印出来,以检查它是否工作正常,之后获得阵列信息的二维数组(还没做完)。但由于某些原因,它给了我段错误,并返回139(0x8B),当我运行它,当我尝试调试它,它打印的一切,但在最后它说,它无法打开文件:LIB-start.c。先谢谢你!多维数组段错误和139(0x8B)返回
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int ***getFileLoad (char *nomeFicheiro);
int ***InitArray (int ***array, int cells);
void freeArray(int ***array, int cells);
int main() {
int ***intArray=NULL, intCells;
char strNome[10], *p2String=NULL;
printf("Nome ficheiro: ");
scanf("%s", strNome);
p2String=strNome;
intArray=getFileLoad(p2String);
intCells=intArray[1][0][0]; /*intCells+2 -> nr of lines to read from the file*/
freeArray(intArray, intCells);
return 0;
}
int ***getFileLoad (char *nomeFicheiro) {
char strLines[5], strColumns[5], strCells[5], strTemp[3];
int i=0, j=0, k=0, b=2, lines=0, columns=0, cells=0, intTemp=0;
int ***array=NULL;
FILE *ficheiro=NULL;
ficheiro=fopen(nomeFicheiro, "r"); /*open file*/
if (ficheiro==NULL) {
printf("Erro ao abrir o ficheiro!\n");
exit(EXIT_FAILURE);
}
fscanf(ficheiro, "%s %s %s", strLines, strColumns, strCells);
if ((lines=atoi(strLines))<=0) {
printf("Invalid nr of lines!\n");
exit(EXIT_FAILURE);
}
if ((columns=atoi(strColumns))<=0) {
printf("Invalid nr of columns!\n");
exit(EXIT_FAILURE);
}
if ((cells=atoi(strCells))<=0) {
printf("Invalid nr of cells!\n");
exit(EXIT_FAILURE);
}
array=InitArray(&array, cells);
array[0][0][0]=lines;
array[0][1][0]=columns;
array[1][0][0]=cells;
for (i=2 ; i<cells+b ; i++) { /*nr of lines to read*/
for (j=0 ; j<1 ; j++) { /*last value*/
for (k=0 ; k<4 ; k++) { /*middle value*/
if (k==3) { /*check if there is something else that I don't want on the Array*/
intTemp=fgetc(ficheiro);
if (intTemp=='\n' || intTemp==EOF) {
continue;
} else {
while(intTemp!='\n' || intTemp!=EOF) {
intTemp=fgetc(ficheiro);
if (intTemp=='\n' || intTemp==EOF) {
break;
}
}
}
} else {
fscanf(ficheiro, "%s", strTemp);
if (isdigit(strTemp)) {
intTemp=atoi(strTemp);
if (k==0) { /*accepts lines between 1->lines*/
if (!(intTemp>=1 && intTemp<lines)) {
printf("Invalid values!\n");
exit(EXIT_FAILURE);
} else {
array[i][k][j]=intTemp;
}
} else if (k==1) { /*accepts columns between 1->columns*/
if (!(intTemp>=1 && intTemp<columns)) {
printf("Invalid values!\n");
exit(EXIT_FAILURE);
} else {
array[i][k][j]=intTemp;
}
} else if (k==2) { /*accepts cells with values between 0->2*/
if (!(intTemp>=0 && intTemp<3)) {
printf("Invalid values!\n");
exit(EXIT_FAILURE);
} else {
array[i][k-1][j+1]=intTemp;
}
}
}
}
}
}
}
intTemp=fgetc(ficheiro); /*checking for EOF*/
if (intTemp!=EOF) {
printf("EOF not detected!\n");
exit(EXIT_FAILURE);
}
fclose(ficheiro);
return array;
}
int ***InitArray (int ***array, int cells) {
int b=2, i=0, j=0, k=0 , l=0;
array=(int ***)malloc((cells+b)* sizeof(int**)); /*allocating 3D array*/
if (array==NULL) {
printf("Not enough memory!\n");
exit(EXIT_FAILURE);
}
for(i=0 ; i<cells+b ; i++) {
(array)[i]=(int **)malloc(b* sizeof(int*));
if ((array)[i]==NULL) {
printf("Not enough memory!\n");
for (j=0 ; j<i ; j++) {
free((array)[j]);
}
free(array);
array=NULL;
exit(EXIT_FAILURE);
}
for (j=0 ; j<b ; j++) {
(array)[i][j]=(int*)malloc(b*sizeof(int));
if ((array)[i][j]==NULL) {
printf("Not enough memory!\n");
for (k=0 ; k<i ; k++) {
for (l=0 ; l<j ; l++) {
free((array)[k][l]);
}
}
free(array);
array=NULL;
exit(EXIT_FAILURE);
}
}
}
return array;
}
void freeArray(int ***array, int cells) {
int i, j, b=2;
for (i=0 ; i<cells+b ; i++) {
for (j=0 ; j<b ; j++) {
free((array)[i][j]);
}
free((array)[i]);
}
free(array);
array=NULL;
}
文件例如:
'intArray = getFileLoad(p2String);'是吗?一个'int'返回到'int ***'? –
请发布[最小,完整和可验证示例](http://stackoverflow.com/help/mcve)。 –
@SouravGhosh,是我不好...... uptading它 – Slyer