如何在二进制数据流中查找字符串? [英] How to find a string in a stream of binary data?

查看:103
本文介绍了如何在二进制数据流中查找字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



你好


我的代码下面以二进制模式打开一个Word文档,并将数据放入

缓冲区。然后我想在这个缓冲区中搜索一个字符串。我尝试使用

strstr但是当它达到第一个空字符或

数据中的一些控制字符时,它会停止查看。我应该用什么C函数才能在BYTE数据缓冲区中搜索


代码:


#include< ; stdio.h>


char szPath [MAX_PATH] ="" ;;

strcpy(szPath," E:\\ MyPath \\ \\\ahl.doc");

FILE * stream;


FILE * file = fopen(szPath," rb"); //打开文件

fseek(文件,0,SEEK_END); //寻求结束

long file_size = ftell(file); //获取当前位置

倒带(文件); //倒回到文件开头


//分配内存以包含整个文件。

BYTE * byBuffer =(BYTE *)malloc(file_size);

// if(buffer == NULL)exit(2);


//将文件复制到缓冲区。

fread(byBuffer,1,file_size,file);


const char * szFind =" selected";

// strstr(StrToLookIn,StrToFind) ;

char * szResult = strstr((char *)byBuffer,szFind);


fclose(file); //关闭文件


免费(byBuffer);

Angus Comber
a *** @ NOSPAMiteloffice.com


Hello

My code below opens a Word document in binary mode and places the data into
a buffer. I then want to search this buffer for a string. I tried using
strstr but think it stops looking when it reaches first null character or
some control character in data. What C function should I use to be able to
search in a BYTE data buffer?
Code:

#include <stdio.h>

char szPath[MAX_PATH] = "";
strcpy(szPath, "E:\\MyPath\\ahl.doc");
FILE* stream;

FILE *file = fopen(szPath, "rb"); // Open the file
fseek(file, 0, SEEK_END); // Seek to the end
long file_size = ftell(file); // Get the current position
rewind (file); // rewind to start of file

// allocate memory to contain the whole file.
BYTE* byBuffer = (BYTE*) malloc (file_size);
// if (buffer == NULL) exit (2);

// copy the file into the buffer.
fread (byBuffer,1,file_size, file);

const char* szFind = "selected";
//strstr(StrToLookIn, StrToFind);
char* szResult = strstr((char*)byBuffer, szFind);

fclose(file); // Close the file

free( byBuffer );
Angus Comber
an***@NOSPAMiteloffice.com

推荐答案

Angus Comber写道:
Angus Comber wrote:

下面的代码以二进制模式打开Word文档,并将数据放入缓冲区。然后我想在这个缓冲区中搜索一个字符串。我尝试使用
strstr但是当它到达第一个空字符或
数据中的一些控制字符时,它会停止查看。我应该使用什么C函数来搜索BYTE数据缓冲区?

代码:

#include< stdio.h>


您需要包含更多内容(例如,malloc的stdlib.h)。
char szPath [MAX_PATH] ="" ;;
strcpy(szPath) ,E:\\ MyPath \\ahl.doc;;
FILE * stream;

FILE * file = fopen(szPath," rb"); //打开文件
fseek(文件,0,SEEK_END); //寻求结束
long file_size = ftell(file); //获取当前位置


嗯,你假设是C99,你可以使用C ++风格的评论,

并且可以在代码中的任何一点?对于

便携性,你最好坚持使用C90。

倒带(文件); //倒回到文件开头
//分配内存以包含整个文件。
BYTE * byBuffer =(BYTE *)malloc(file_size);


看看这个新闻组的档案,你会看到许多好的

原因_not_从malloc投出回报,如果你是写作

in C.

// if(buffer == NULL)exit(2);

My code below opens a Word document in binary mode and places the data into
a buffer. I then want to search this buffer for a string. I tried using
strstr but think it stops looking when it reaches first null character or
some control character in data. What C function should I use to be able to
search in a BYTE data buffer?

Code:

#include <stdio.h>
You need to include more than this (e.g. stdlib.h for malloc).
char szPath[MAX_PATH] = "";
strcpy(szPath, "E:\\MyPath\\ahl.doc");
FILE* stream;

FILE *file = fopen(szPath, "rb"); // Open the file
fseek(file, 0, SEEK_END); // Seek to the end
long file_size = ftell(file); // Get the current position
Hmm, you''re assuming C99, where you can use C++-style comments,
and can introduce new variables at any point in the code? For
portability, you''re best to stick with C90.
rewind (file); // rewind to start of file

// allocate memory to contain the whole file.
BYTE* byBuffer = (BYTE*) malloc (file_size);
Look at the archive of this newsgroup, and you''ll see many good
reasons _not_ to cast the return from malloc, if you''re writing
in C.
// if (buffer == NULL) exit (2);




出口(2)??这是什么类型的代码?


好​​吧,无论如何,这是一个ISO / ANSI C程序(我认为)将会b / b
做你想要的,不一定效率最高。


#include< stdio.h>

#include< stdlib.h>

#include< string.h>


long find_string_in_buf(unsigned char * buf,size_t len,

const char * s)

{

long i,j;

int slen = strlen(s);

long imax = len - slen - 1;

long ret = -1;

int match;


for(i = 0; i< imax; i ++){

match = 1;

for(j = 0; j< slen; j ++){

if(buf [i + j]!= s [j]){

匹配= 0;

休息;

}

}

if(match){

ret = i;

break;

}

}


返回ret;

}


int main(int argc,char ** argv)

{

const char * targ ="已选中" ;;

const char * fname;

FILE * fp;

size_t file_size;

unsigned char * buf;

long loc;


if(argc< 2){

fputs(请提供文件名\ n,stderr);

退出(EXIT_FAILURE);

}


fname = argv [1];


fp = fopen(fname," rb");

if(fp == NULL){

fprintf(stderr,无法打开''%s''\ n,fname);

退出(EXIT_FAILURE);

}


fseek(fp,0,SEEK_END);

file_size = ftell(fp);

倒带(fp);


buf = malloc(file_size);

if(buf == NULL){

fputs(Out of memory\\\
,stderr);

退出(EXIT_FAILURE);

}


fread(buf,1,file_size,fp);


loc = find_string_in_buf(buf,file_size,targ);

if(loc< ; 0){

printf("目标字符串''%s''未找到\ n",targ);

}其他{

printf("目标字符串''%s''在字节%ld \ n"处找到,

targ,loc);

}


fclose(fp);

free(buf);


返回0;

}


-

Allin Cottrell

经济系

北卡罗来纳州维克森林大学



exit(2)?? What sort of code is that?

Well, anyway, here is an ISO/ANSI C program that (I think) will
do what you want, not necessarily with greatest efficiency.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

long find_string_in_buf (unsigned char *buf, size_t len,
const char *s)
{
long i, j;
int slen = strlen(s);
long imax = len - slen - 1;
long ret = -1;
int match;

for (i=0; i<imax; i++) {
match = 1;
for (j=0; j<slen; j++) {
if (buf[i+j] != s[j]) {
match = 0;
break;
}
}
if (match) {
ret = i;
break;
}
}

return ret;
}

int main (int argc, char **argv)
{
const char *targ = "selected";
const char *fname;
FILE *fp;
size_t file_size;
unsigned char *buf;
long loc;

if (argc < 2) {
fputs("Please supply a filename\n", stderr);
exit(EXIT_FAILURE);
}

fname = argv[1];

fp = fopen(fname, "rb");
if (fp == NULL) {
fprintf(stderr, "Couldn''t open ''%s''\n", fname);
exit(EXIT_FAILURE);
}

fseek(fp, 0, SEEK_END);
file_size = ftell(fp);
rewind(fp);

buf = malloc(file_size);
if (buf == NULL) {
fputs("Out of memory\n", stderr);
exit(EXIT_FAILURE);
}

fread(buf, 1, file_size, fp);

loc = find_string_in_buf(buf, file_size, targ);
if (loc < 0) {
printf("The target string ''%s'' was not found\n", targ);
} else {
printf("The target string ''%s'' was found at byte %ld\n",
targ, loc);
}

fclose(fp);
free(buf);

return 0;
}

--
Allin Cottrell
Department of Economics
Wake Forest University, NC




" Angus Comber" <一个*** @ iteloffice.com.PLEASENOSPAM>在消息中写道

news:40 *********************** @ mercury.nildram.net ...

"Angus Comber" <an***@iteloffice.com.PLEASENOSPAM> wrote in message
news:40***********************@mercury.nildram.net ...

您好

下面的代码以二进制模式打开Word文档,并将数据
放入缓冲区。然后我想在这个缓冲区中搜索一个字符串。我尝试使用
strstr但是当它到达第一个空字符或
数据中的一些控制字符时,它会停止查看。我应该用什么C函数来
来搜索BYTE数据缓冲区?

代码:

#include< stdio.h>

char szPath [MAX_PATH] ="" ;;
strcpy(szPath," E:\\MyPath\\ahl.doc");
FILE * stream ;

FILE * file = fopen(szPath," rb"); //打开文件
fseek(文件,0,SEEK_END); //寻求结束
long file_size = ftell(file); //获取当前位置
倒带(文件); //回退到文件开头

//分配内存以包含整个文件。
BYTE * byBuffer =(BYTE *)malloc(file_size);
// if( buffer == NULL)exit(2);

//将文件复制到缓冲区。
fread(byBuffer,1,file_size,file);

const char * szFind =" selected";
// strstr(StrToLookIn,StrToFind);
char * szResult = strstr((char *)byBuffer,szFind);

FCLOSE(文件); //关闭文件

免费(byBuffer);

Angus Comber
***@NOSPAMiteloffice.com



#include< stdio.h>

#include< string.h>


#define BUFFER_SIZE 200 / *根据您的需要调整* /

/ * nz_str()* /

/ * * /

/ *表现为''strstr()'',但用* /

/ *嵌入零字符处理输入* /

/ * * /

/ *''data''和'to_find''必须为零终止* /

char * nz_strstr(char * data,const char * to_find)< br $>
{

char * result = 0;


while(!(result = strstr(data,to_find)))

数据+ = strlen(数据)+ 1;


返回结果;

}


int main(int argc,char ** argv)

{

char buffer [BUFFER_SIZE] =" Hello world \0这是\ 0一个测试" ;;

char what [] =" test";

char * p = nz_strstr(buffer,what);


printf(" string''%s' '",what);


if(p)

printf(" found at offset%lu \ n",(unsigned long)( p - buffer));

else

printf(not found\\\
);

返回0;

}


-Mike


#include <stdio.h>
#include <string.h>

#define BUFFER_SIZE 200 /* adjust to your needs */
/* nz_str() */
/* */
/* behaves as ''strstr()'' but handles input with */
/* embedded zero characters */
/* */
/* ''data'' and ''to_find'' must be zero-terminated */
char *nz_strstr(char *data, const char *to_find)
{
char *result = 0;

while(!(result = strstr(data, to_find)))
data += strlen(data) + 1;

return result;
}

int main(int argc, char **argv)
{
char buffer[BUFFER_SIZE] = "Hello world\0 this is\0 a test";
char what[] = "test";
char *p = nz_strstr(buffer, what);

printf("string ''%s'' ", what);

if(p)
printf("found at offset %lu\n", (unsigned long)(p - buffer));
else
printf("not found\n");
return 0;
}

-Mike


Allin Cottrell写道:
Allin Cottrell wrote:
嗯,你在假设C99,在那里你可以使用C ++风格的评论,
并且可以在代码的任何一点引入新的变量?
为了便携,你是最好的坚持使用C90。
Hmm, you''re assuming C99, where you can use C++-style comments,
and can introduce new variables at any point in the code?
For portability, you''re best to stick with C90.




如果有的话,你会建议继续使用C 99吗?



When, if ever, would you recommend moving on to C 99?

这篇关于如何在二进制数据流中查找字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆