您好,我有一個關於讀取pgm值的底部值的問題。我已經創建了一個2d的dyanmic數組,並且已經爲高度和寬度添加了2個額外的空間,以便能夠創建一個緩衝區當我試圖讀取第一個整數到寬度+ 1它不起作用。任何線索爲什麼代碼停在這裏?閱讀一個pgm文件
FILE *pfile; //pointer to file
pfile=fopen(input_pgm,"r");
int columns,rows,value,line_of_numbers;
char pformat[50];
if (pfile==NULL){
printf("File is empty lame-o\n");
}
else{ //Read from file
fscanf(pfile,"%s",pformat); //Things like P2,P5
fscanf(pfile,"%d",&columns); //Number of columns ,M, Need & or it will store it into address
fscanf(pfile,"%d",&rows); //Number of rows , N
fscanf(pfile,"%d",&value); //Number of value, W
//Allocating 2D array to store M numbers
//printf("Test");
int **board;
board = (int **)malloc((rows + 2) * sizeof(int *));
for (i=0;i<rows+2;i++){
board[i]=(int*)malloc((columns+2)*sizeof(int));
}
//printf("Test2");
//INITILIZING TO 0
board=memset(board,0,((columns+2)*(rows+2)));
//printf("Test");
//reading in numbers on to board from 1->/rows/columns+1
for(i=1;i<=rows+1;i++){
for(j=1;j<=columns+1;j++){
//printf("Test3"); /*does not work after this*/
int scanned_num;
fscanf(pfile,"%d",&scanned_num);
board[i][j]=scanned_num;}
}
問題可能在任何地方。請閱讀如何提供[mcve]。 – user694733
請注意,當'fopen()'返回NULL時,這意味着該文件無法打開,**不**該文件爲空。它可能不存在,或者程序可能沒有被授權訪問它,或者可能有其他一些問題。可以打開一個空文件,如果你通過'fopen()'成功完成,那麼它返回的指針是* not *'NULL'。 –
定義「不起作用」。 –