strcmp 表现得很奇怪
我有一个小程序,它将在文件中搜索某些字符串。 该字符串末尾有一个可变部分,并且前面始终有一个表示大小的字节。
例如,我们将在“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您忘记以空终止缓冲区。或者,您应该使用 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.