我試圖從duom.txt文件讀取文本並將每個字符存儲到數組中。 但我沒有得到正確的答案。 我的代碼有什麼問題?如何從文本文件中讀取文本並將每個字符存儲在數組中
# include <stdio.h>
# include <stdlib.h>
int main()
{
FILE *in;
char ch,str[100],cw;
int j,i = 0;
in=fopen("duom.txt","r");
if(in){
while(!feof(in)){
ch=getc(in);
str[i] = ch;
i++;
}
}
for(j=0;j<i;j++){
printf("%c",str[i]);
}
printf("\n");
fclose(in);
system("pause");
return 0;
}
duom.txt文件:
My name is Lukas
我知道這是不是回答你的問題,但你的代碼格式是令人厭惡的。嘗試並實踐適當的縮進和句法「美化」,以免您未來可能會迷惑自己。 – tsujp 2013-02-16 17:08:43