[问题] 有关strstr()

楼主: momo81 (毛毛)   2014-09-28 02:22:20
开发平台(Platform): (Ex: VC++, GCC, Linux, ...)
dev c
额外使用到的函数库(Library Used): (Ex: OpenGL, ...)
问题(Question):
如程式码
想请问有什么原因会这样
谢谢
喂入的资料(Input):
strstr("This is a",读取的资料);
预期的正确结果(Expected Output):
搜寻的到
错误结果(Wrong Output):
搜不到
程式码(Code):(请善用置底文网页, 记得排版)
FILE *fPtr = fopen("A.txt","r");
char *b;
char txtvalue[10];
fgets(txtvalue, 10, fPtr);
b=strstr("This is a",txtvalue);
if(b!=0)
{
printf("%s",b);
}
fclose(fPtr);
system("PAUSE");
return 0;
A.txt里面的内容
is
a
b
这样子的话 找不到
但是只要把 strstr("This is a",txtvalue);的txtvalue写死成"is"
就找的到..
补充说明(Supplement):
作者: prismwu   2014-09-28 02:25:00
PO 你写得出来 这边不是占卜板...
作者: scwg ( )   2014-09-28 02:36:00
fgets 会连换行一起读进来, 相当于 "is\n". 要手动把\n盖掉
作者: PoorLoser (废文制造机)   2014-09-28 02:45:00
http://www.cplusplus.com/reference/cstring/strstr/要比对的字串放后面看错, 忘了它吧...
作者: scwg ( )   2014-09-28 07:30:00
改过的程式码是什么样的? 用哪个作业系统? A.txt 有 BOM 吗?我用 txtvalue[strlen(txtvalue) - 1] = 0; 找得到

Links booklink

Contact Us: admin [ a t ] ucptt.com