strstr(char * str1,char * str2) [英] strstr(char *str1, char *str2)
问题描述
你好,
我是这个C的新手 - 从来没有 - 我正在尝试搜索字符串
发生子串 - 但是,由于我使用
,因此strstr函数总是返回NULL,所以我不是很成功。但是我知道有一些子字符串存在,因为当我打印出要搜索的字符串
时,我可以用自己的双眼看到它。我添加了函数的代码(resolveResponse)以及我如何
调用此代码 -
调用函数:
- ---
resolveResponse(缓冲区);
----
缓冲区是这样定义的;
----
char缓冲区[8192];
----
函数调用
----
char * resolveResponse(char * buffer){
char * endOfResponse; char * startOfFile;
printf(缓冲区);
endOfResponse = strstr(buffer," ****"); / *< ---总是NULL * /
if(endOfResponse == NULL){
printf(" \ nEnd not found!\ n") ;
}
startOfFile = endOfResponse + 4;
返回startOfFile;
}
非常感谢你的帮助!
祝你好运Lars
Hi there,
I''m new to this C - never the less - I''m trying to search a string for the
occurence of a substring - However, I''m not very succesful since my use of
the strstr function always returns NULL. But I know that there exsists such
a substring, as I can see it with my own two eye when I print out the string
to be searched. I added the code of the function (resolveResponse)and how I
call this code -
Calling the function:
----
resolveResponse(buffer);
----
buffer is defined this way;
----
char buffer[8192];
----
The Function called
----
char *resolveResponse(char *buffer) {
char *endOfResponse; char *startOfFile;
printf(buffer);
endOfResponse = strstr(buffer, "****"); /* <--- Always NULL */
if(endOfResponse == NULL) {
printf("\nEnd not found!\n");
}
startOfFile = endOfResponse+4;
return startOfFile;
}
Your help is much appreciated!
Best regards Lars
推荐答案
" Lars Langer < 11 ***** @ uwo.caREMOVETHIS>写道:
"Lars Langer" <ll*****@uwo.caREMOVETHIS> writes:
我是这个C的新手 - 从来没有 - 我正在尝试搜索字符串以查找子串的出现 - 但是,我不是自从我使用strstr函数以来非常成功,总是返回NULL。但我知道存在这样的子串,因为当我打印出要搜索的字符串时,我可以用自己的双眼看到它。我添加了函数的代码(resolveResponse)以及我如何调用这段代码 -
调用函数:
----
resolveResponse(buffer);
----
缓冲区是这样定义的;
----
char buffer [8192];
----
的函数----
char * resolveResponse(char * buffer){
char * endOfResponse; char * startOfFile;
printf(缓冲区);
endOfResponse = strstr(buffer," ****"); / *< ---始终为NULL * /
if(endOfResponse == NULL){
printf(" \ nEnd not found!\\\
");
}
startOfFile = endOfResponse + 4;
返回startOfFile;
}
I''m new to this C - never the less - I''m trying to search a string for the
occurence of a substring - However, I''m not very succesful since my use of
the strstr function always returns NULL. But I know that there exsists such
a substring, as I can see it with my own two eye when I print out the string
to be searched. I added the code of the function (resolveResponse)and how I
call this code -
Calling the function:
----
resolveResponse(buffer);
----
buffer is defined this way;
----
char buffer[8192];
----
The Function called
----
char *resolveResponse(char *buffer) {
char *endOfResponse; char *startOfFile;
printf(buffer);
endOfResponse = strstr(buffer, "****"); /* <--- Always NULL */
if(endOfResponse == NULL) {
printf("\nEnd not found!\n");
}
startOfFile = endOfResponse+4;
return startOfFile;
}
我们无法知道'是什么'在缓冲区中或者它是如何初始化的。
。如果您的程序正在打印未找到结束!,则
缓冲区不包含****的出现,或其他内容为
出错了。你没有给我们足够的信息来猜测。
如果可以的话,把问题减少到一个小的自包含程序,
我们可以剪切并粘贴并尝试自己。向我们展示修剪过的
程序及其输出。 (你很有可能在这样做的时候自己找到
的问题。)
另外,printf(缓冲区); "声明可能不安全。 printf()的第一个
参数是一个格式字符串;如果你的缓冲区包含任何
''%''个字符,printf很可能会尝试打印其他参数
(并且没有)。 />
试试这个:
printf("%s",buffer);
或者这个:
fputs(缓冲区,标准输出);
-
Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>
San迭戈超级计算机中心< *> < http://users.sdsc.edu/~kst>
我们必须做点什么。这是事情。因此,我们必须这样做。
We have no way of knowing what''s in "buffer" or how it was
initialized. If your program is printing "End not found!", either
buffer doesn''t contain an occurrence of "****", or something else is
going wrong. You haven''t given us enough information to guess.
If you can, reduce the problem to a small self-contained program,
something we can cut-and-paste and try ourselves. Show us the trimmed
program and its output. (There''s a good chance you''ll find the
problem yourself while doing this.)
Also, the "printf(buffer);" statement is probably unsafe. The first
argument to printf() is a format string; if your buffer contains any
''%'' characters, printf is likely to try to print its other arguments
(and there aren''t any).
Try this:
printf("%s", buffer);
Or this:
fputs(buffer, stdout);
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Keith Thompson写道:
Keith Thompson wrote:
fputs(buffer,stdout);
fputs(buffer, stdout);
或者这个:puts(缓冲区);
-
Derrick Coetzee
Or this: puts(buffer);
--
Derrick Coetzee
在星期五,2004年9月24日18:13:12 -0400,Lars Langer
< ll ***** @ uwo.caREMOVETHIS>写道:
On Fri, 24 Sep 2004 18:13:12 -0400, "Lars Langer"
<ll*****@uwo.caREMOVETHIS> wrote:
你好,
我是这个C的新手 - 从来没有 - 我正在尝试搜索字符串中的
子串的出现 - 但是,由于我使用strstr函数总是返回NULL,所以我不是很成功。但我知道存在这样的子串,因为当我打印出要搜索的字符串时,我可以用自己的双眼看到它。我添加了函数的代码(resolveResponse)以及我如何调用这段代码 -
调用函数:
----
resolveResponse(buffer);
----
缓冲区是这样定义的;
----
char buffer [8192];
----
的函数----
char * resolveResponse(char * buffer){
char * endOfResponse; char * startOfFile;
printf(buffer);
向我们展示你在这里看到的一个例子。
endOfResponse = strstr(缓冲区,****); / *< --- Always NULL * /
if(endOfResponse == NULL){
printf(" \ nEnd not found!\ n");
你需要类似
返回NULL;
这里因为如果你在这段代码中下一行不做什么
你期待。
}
startOfFile = endOfResponse + 4;
返回startOfFile;
}
Hi there,
I''m new to this C - never the less - I''m trying to search a string for the
occurence of a substring - However, I''m not very succesful since my use of
the strstr function always returns NULL. But I know that there exsists such
a substring, as I can see it with my own two eye when I print out the string
to be searched. I added the code of the function (resolveResponse)and how I
call this code -
Calling the function:
----
resolveResponse(buffer);
----
buffer is defined this way;
----
char buffer[8192];
----
The Function called
----
char *resolveResponse(char *buffer) {
char *endOfResponse; char *startOfFile;
printf(buffer);
Show us an example of what you see here.
endOfResponse = strstr(buffer, "****"); /* <--- Always NULL */
if(endOfResponse == NULL) {
printf("\nEnd not found!\n");
You need something like
return NULL;
here because if you are in this code the next line does not do what
you expect.
}
startOfFile = endOfResponse+4;
return startOfFile;
}
<<删除电子邮件的del>>
<<Remove the del for email>>
这篇关于strstr(char * str1,char * str2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!