我一直在使用strncmp和getline过期问题......所以我做了一个小测试应用程序。 strncmp给出了误报,我无法弄清楚我做错了什么。strncmp()给出了一个假阳性
这是程序
#include <stdio.h>
FILE *fp=NULL;
char uname[4]="test";
char *confline=NULL;
size_t conflinelength=0;
int datlength;
main()
{
datlength=4;
fp=fopen("test.txt","r+");
while (getline(&confline,&conflinelength,fp)!=-1)
{
if (strncmp(confline,uname,(datlength-1))==0);
{
printf("match\n");
return;
}
}
printf("no match\n");
fclose(fp);
}
这就是 “test.txt的” ...
bjklas
和节目输出
match
'炭UNAME [4] = “test”;'可以作为'datlength = 4',但建议'char uname [] =“test”;'。 'printf(“match \ n”);'后面应该跟'fclose(fp);'。 – chux