【发布时间】:2021-03-09 13:02:33
【问题描述】:
我的代码在这里
int main(){
FILE *fp;
fp = fopen("dic.txt", "r");
while(getc(fp) != EOF){
if(getc(fp) == ' '){
printf("up ");
}
}
}
我的 dic.txt 在这里
我的预测是“涨涨涨涨”
因为,有四个空格“”
但它只打印了一个“up”
什么问题?
【问题讨论】:
-
将文本文件作为文本发布到代码块中。不是图片。
-
@klutt 抱歉,算了,评论已删除。谢谢
-
关于:
while(getc(fp) != EOF){ if(getc(fp) == ' '){每次循环输入两个字符。建议:int ch; while( (ch = getc(fp)) != EOF){ if(ch == ' '){ -
关于:
fp = fopen("dic.txt", "r");对于健壮的代码,请始终检查 (!=NULL) 返回值以确保操作成功。建议:fp = fopen("dic.txt", "r"); if( ! fp ) { perror( "fopen to read dic.txt failed" ); exit( EXIT_FAILURE ); }