C语言,从外部文件读取换行符

C language, read new line character from external file

大家好,我有一个这样的外部文件:

我想将所有字符(包括 space 和新行)读取到我的二维(行和列)数组中,但我没有这样做。例如,在该文件的第 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;
}

如果我的算法有问题,你能告诉我怎么做吗?我的目标是将所有具有该位置的字符复制到我的数组中。

你的if逻辑错误,if c == 0 (else condition)永远不会换行。