2011-06-26 46 views
0

我有一个小程序,它将搜索文件中的一些字符串。 这个字符串在结尾处有一个可变的部分,并且总是在一个字节之前告诉大小。在:strcmp行为怪异

例如,我们将寻找 “HTTP //” “aaaaa.http://www.example.combbbbb”(的ASCII码是0×17

比方说,我们 “”。 。打开文件 的代码被执行为:

while(car != EOF){ 
    car = fgetc(file[ii]); // we get everything in the file 
    lastBuffStart=ftell(file[ii]); 
    ij=1; 
    buffer[0]=car; // we start editing the buffer 
    printf("\n%d (%c) - %d (%c) ",car,car,base[0],base[0]); 
    while(ij<(buffsize-1)){ 
     buffer[ij]=fgetc(file[ii]); 
     printf("\n | %d (%c) - %d (%c) ",buffer[ij],buffer[ij],base[ij],base[ij]); 
     ij++; 
    } 

    fseek(file[ii],lastBuffStart,0); // we get back to the old position before the buffer continues 

    if(strcmp(buffer,base)==0){ // we compare 
     byteSize = (ftell(file[ii])-1); // we get the position of the size byte 
     printf("\nFound : 0x%x\n",byteSize); 
     } 
    } 

我们读到的所有文件,并把在缓冲区中下一个字符的基础比较(在HTTP://)。

我的问题是如果我们删除printf(“\ n |%d(%c) - %d(%c)”,buffer [ij],buffer [ij],base [ij],base [ij]); 什么都没有发现 ...

我真的不明白我做错了什么。

你能帮我吗?

在此先感谢。

回答

3

您忘记了空终止缓冲区。或者,您应该使用memcmp而不是strcmp。此外,如果您使用fread而不是while循环,代码将会更清晰。

+0

谢谢。这很简单。我不知道为什么我没有看到。 – MisterDoy