C语言,从外部文件中读取新行字符
问题描述:
你好,大家好我有一个外部文件中像这样:C语言,从外部文件中读取新行字符
我想读的所有字符(包括空间和新的生产线),以我的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是拨错,你能告诉我怎么了?我的目标是将所有人物的位置复制到我的阵列中。
答
如果逻辑错误,如果c == 0(否则条件)它将永远不会成为新行。
['int c ;'!](http://stackoverflow.com/questions/35356322/difference-between-int-and-char-in-getchar-fgetc-and-putchar-fputc) –
我试过,它没有工作 –
加布里埃尔的回答是正确的,我的是对你的代码的批评。 'getchar'返回* int *,而不是'char'。 –