我一直在使用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