[英]C language help:How to store strings from a .txt file into a character array? This .txt file is read from a command line argument btw.
[英]C language, read new line character from external file
大家好,我有一个这样的外部文件:
我想将所有字符(包括空格和换行符)读取到我的二维(行和列)数组中,但是我无法做到这一点。 例如,在该文件的第10行和第1列(左下角)中,有一个'/'字符。 我想将'/'放在我的array [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逻辑错误,如果c == 0(其他条件),它将永远不会是新行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.