テキスト ファイルから char 配列に行を読み込もうとしていますが、何か問題があります。コードを見て、何が間違っているのか教えてください。ありがとう。
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int i=0,j;
char* string[100];
char line[100];
FILE *file;
file = fopen("patt", "r");
while(fgets(line, sizeof line, file)!=NULL) {
printf("%d %s",i, line);
string[i]=line;
i++;
}
for (j=0 ; j<i ; j++) {
printf("string[%d] %s",j, string[j]);
}
fclose(file);
return 0;
}
入力ファイル patt の内容は次のとおりです。
rec
cent
ece
ce
recent
nt
上記のコードを実行すると、これが得られます
0 rec
1 cent
2 ece
3 ce
4 recent
5 nt
string[0] nt
string[1] nt
string[2] nt
string[3] nt
string[4] nt
string[5] nt
私が期待するのはこれです
0 rec
1 cent
2 ece
3 ce
4 recent
5 nt
string[0] rec
string[1] cent
string[2] ece
string[3] ce
string[4] recent
string[5] nt