0$房天下问答|C语言程序,请指教。$https://m.fang.com/ask/ask_1401437.html$https://static.soufunimg.com/common_m/m_public/201511/images/asksharedefault.png
packc/pages/ask/detail/detail?askid=1401437
-
C语言程序,请指教。
#include <stdio.h>int main(){FILE *fp; int i; char str[10]; char text[10]; if((fp=fopen("err.txt","rb"))==NULL) {printf("cannot!\n");exit(0);} while(fgets(str,10,fp)!=NULL) { while(fgets(text,10,fp)!=NULL) { if(text==str) printf("%s",str); } } fclose(fp);}其中,c文档中的内容如下:hghgghujhghyhykuil现在要把重复的数据筛选出来,请问,我这个程序问题出在哪,谢谢了!
更多
共1个回答
-
-
-
updown_girl
丨Lv 2
//这是按照楼主的思路的答案楼主的几个问题:1:没有考虑文件指针当前位置,每次做完内层循环应该重置一下当前指针位置;2:字符串比较问题;#include <stdio.h> #include <stdlib.h>#include <string.h>int main() { FILE *fp; int i; char str[10]; char text[10]; long pos; if((fp=fopen("err.txt","rb"))==NULL) { printf("cannot!\n"); exit( 1 );; } while(fgets(str,10,fp)!=NULL) { pos=ftell(fp); while(fgets(text,10,fp)!=NULL) { if(0==strcmp(text,str)) printf("%s",str); } fseek(fp,pos,SEEK_SET); } fclose(fp); } /*结果:hghy请按任意键继续. . .*/
免责声明:问答内容均来源于互联网用户,房天下对其内容不负责任,如有版权或其他问题可以联系房天下进行删除。

关注成功