strcmp 表现得很奇怪

发布于 2024-11-17 08:18:30 字数 1121 浏览 4 评论 0原文

我有一个小程序,它将在文件中搜索某些字符串。 该字符串末尾有一个可变部分,并且前面始终有一个表示大小的字节。

例如,我们将在“aaaaa.http://www.example.combbbbb”中查找“http://”(“.”的ASCII码是0x17。

假设我们已经打开了该文件。 要执行的代码是:

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]); 什么也没发现...

我真的看不出我做错了什么。

你能帮助我吗 ?

提前致谢。

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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽容 2024-11-24 08:18:30

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文