开发者

strcmp acts weird

I have a little program which will search for some string in a file. This string have a variable part on the end and is always preceded by a byte wich tell the size.

For instance, we will be looking for "http://" in "aaaaa.http://www.example.combbbbb" (the ASCII code of "." is 0x17.

Let's say we have opened the file. The code to be executed is :

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开发者_如何学Go 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);
         }
    }

We read all the file and put in a buffer the next characters to compare with the base (the http://).

My problem is if we remove the printf("\n | %d (%c) - %d (%c) ",buffer[ij],buffer[ij],base[ij],base[ij]); nothing is found...

I really can't see what I am doing wrong.

Can you help me ?

Thanks in advance.


You forgot to null-terminate the buffer. Alternatively, you should use memcmp instead of strcmp. Also, the code would be much clearer had you used fread instead of a while loop.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜