2
當我使用導入到Xcode的文件運行此程序時,我在c = fgetc(ptr);
行收到Thread 1: EXC_BAD_ACCESS (code=1 address=0x68)
的錯誤。我不知道爲什麼fptr
在它到達這一行時爲空。任何幫助將非常感謝。使用帶有Xcode的文件的問題c
#include <stdio.h>
#define M 100
#define N 100
char array[M][N] = {0};
void readFile();
void findStartandEnd();
void mazeTraversal();
int m = 0;
int n = 0;
int start[2] = {0};
int end[2] = {0};
int main() {
readFile();
//findStartandEnd();
//mazeTraversal();
}
void readFile() {
FILE *fptr;
char c;
char file_name[20];
int i, j;
printf("Please enter the size of the MxN maze.\n");
printf("Start with the M size then the N size follow each number by the return key.\n");
scanf("%d", &m);
scanf("%d", &n);
printf("Type in the name of the file containing the Field\n");
scanf("%s", file_name);
fptr = fopen(file_name, "r");
for (i = 0; i < M && i < m; i++)
for (j = 0; j < N && j < n; j++) {
c = fgetc(fptr);
while (!((c == '1') || (c =='0')))
c = fgetc(fptr);
array[i][j] = c;
}
fclose(fptr);
for (i = 0; i < M && i < m; i++)
for (j = 0; j < N && j < n; j++) {
if (j == 0) printf("\n");
printf("%c ", array[i][j]);
}
printf("\n");
}
您是否檢查過驗證'fptr'是否有效?在取消引用可能是NULL指針之前,確實應該確保文件已成功打開。 –
@ChadBrown:你爲什麼回滾編輯? – chqrlie
'void readFile();'等不是有效的原型,並使用不贊成使用的語法。 – Olaf