![](/img/trans.png)
[英]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.