2017-03-04 101 views
0

你好,大家好我有一個外部文件中像這樣:C語言,從外部文件中讀取新行字符

screenshot

我想讀的所有字符(包括空間和新的生產線),以我的2維(行和列)數組,但我沒有這樣做。例如,在第10行和第1列(左下角)的文件中有一個'/'字符。我想在我的數組[9] [0]中放入'/'(數組中的索引從0開始,不是1),所以每個字符都與文件一樣位於數組中。

這裏是我的代碼:

#include <stdio.h> 

int main(void) { 
/** my index array variable, i for row and j for column */ 
int i; 
int j; 

/** varible to read character */ 
char c; 

/** array for the input. The file will not consist more than 17 rows and 2017 columns*/ 
char input[17][2017]; 

/** pointer to a file */ 
FILE *fp; 

/** read that file. (my code and that file are located in the same place) */ 
fp = fopen("bangun.in", "r"); 

/** start reading the file */ 
i = 0; 
j = 0; 
while ((c = fgetc(fp)) != EOF){ 
     if (c != 0) { 
      input[i][j]=c; 
     } 
     else if (c == '\n') { 
      input[i][j]=c; 
      printf("get in"); 
      i++; 
     } 
    j++; 
} 
fclose(fp); 
return 0; 
} 

如果我的algoritm是撥錯,你能告訴我怎麼了?我的目標是將所有人物的位置複製到我的陣列中。

+1

['int c ;'!](http://stackoverflow.com/questions/35356322/difference-between-int-and-char-in-getchar-fgetc-and-putchar-fputc) –

+0

我試過,它沒有工作 –

+0

加布裏埃爾的回答是正確的,我的是對你的代碼的批評。 'getchar'返回* int *,而不是'char'。 –

回答

4

如果邏輯錯誤,如果c == 0(否則條件)它將永遠不會成爲新行。