By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,158 Members | 1,006 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,158 IT Pros & Developers. It's quick & easy.

strstr(char *str1, char *str2)

P: n/a
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
Nov 14 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
"Lars Langer" <ll*****@uwo.caREMOVETHIS> writes:
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;
}


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.
Nov 14 '05 #2

P: n/a
Keith Thompson wrote:
fputs(buffer, stdout);


Or this: puts(buffer);
--
Derrick Coetzee
Nov 14 '05 #3

P: n/a
On Fri, 24 Sep 2004 18:13:12 -0400, "Lars Langer"
<ll*****@uwo.caREMOVETHIS> wrote:
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;
}


<<Remove the del for email>>
Nov 14 '05 #4

P: n/a
Derrick Coetzee <dc****@moonflare.com> writes:
Keith Thompson wrote:
fputs(buffer, stdout);


Or this: puts(buffer);


Which is find if you don't mind an extra newline at the end of the
output.

--
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.
Nov 14 '05 #5

P: n/a
Keith Thompson <ks***@mib.org> writes:
Derrick Coetzee <dc****@moonflare.com> writes:
Keith Thompson wrote:
fputs(buffer, stdout);


Or this: puts(buffer);


Which is find if you don't mind an extra newline at the end of the
output.


More badd proffredding.

That should be, "Which is fine if ...".

--
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.
Nov 14 '05 #6

P: n/a
Lars Langer wrote:
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;
}


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

#define BUFFERL 8192
char *resolveResponse(char *buffer);
int main(void)
{
char buffer[BUFFERL] = "The end of the response is marked "
"with ****Data follows.";
char *data;
data = resolveResponse(buffer);
printf("Returned value is %p,\n pointing to \"%s\"\n",
(void *) data, data);
return 0;
}
char *resolveResponse(char *buffer)
{
char *endOfResponse;
char *startOfFile;

printf("%s\n", buffer);

if (!(endOfResponse = strstr(buffer, "****")))
printf("\nEnd not found!\n");
startOfFile = endOfResponse + 4;

return startOfFile;
}

[Output]

The end of the response is marked with ****Data follows.
Returned value is ee003,
pointing to "Data follows."
Nov 14 '05 #7

P: n/a
Thank you for your answers ;o)

Martin, if you have the time you might explain to me what the error was...?
I'm still not sure I get it - I'm still at bit slow on C. - Anyway this code
solved the problem.... Thank you!

Best regards
Lars
"Martin Ambuhl" <ma*****@earthlink.net> wrote in message
news:2r*************@uni-berlin.de...
Lars Langer wrote:
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;
}


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

#define BUFFERL 8192
char *resolveResponse(char *buffer);
int main(void)
{
char buffer[BUFFERL] = "The end of the response is marked "
"with ****Data follows.";
char *data;
data = resolveResponse(buffer);
printf("Returned value is %p,\n pointing to \"%s\"\n",
(void *) data, data);
return 0;
}
char *resolveResponse(char *buffer)
{
char *endOfResponse;
char *startOfFile;

printf("%s\n", buffer);

if (!(endOfResponse = strstr(buffer, "****")))
printf("\nEnd not found!\n");
startOfFile = endOfResponse + 4;

return startOfFile;
}

[Output]

The end of the response is marked with ****Data follows.
Returned value is ee003,
pointing to "Data follows."

Nov 14 '05 #8

P: n/a
Lars Langer wrote:
Thank you for your answers ;o)

Martin, if you have the time you might explain to me what the error was...?


I haven't any idea what the error was, since it was almost certainly in
the calling code. Note that I left an error. The code
char *resolveResponse(char *buffer)
{
char *endOfResponse;
char *startOfFile;

printf("%s\n", buffer);

if (!(endOfResponse = strstr(buffer, "****")))
printf("\nEnd not found!\n");
startOfFile = endOfResponse + 4;

return startOfFile;
}


does not handle properly the case where "****" is not found. The
problem is that when endOfResponse is NULL the expression
'endOfResponse+4' is meaningless. Consider this tiny pair of changes:

char *resolveResponse(char *buffer)
{
char *endOfResponse;
char *startOfFile = 0; /* initialize startOfFile */

printf("%s\n", buffer);

if (!(endOfResponse = strstr(buffer, "****")))
printf("\nEnd not found!\n");
else /* add so startOfFile is changed only if "****"
is found */
startOfFile = endOfResponse + 4;

return startOfFile;
}

Now you will return NULL if the endOfResponse is changed and should
check for it in the caller. Since you have that flag available, you can
move the output printf into the calling code. It is generally a good
idea to try having I/O not spread out among functions. The routine can
now collapse to

char *resolveResponse(char *buffer)
{
char *endOfResponse;
char *startOfFile = 0; /* initialize startOfFile */

if ((endOfResponse = strstr(buffer, "****")))
startOfFile = endOfResponse + 4; /* change if "****" found */

return startOfFile;
}

This is a very trivial program, and so wants to really be inlined. If
you mave the keyword 'inline' available with your compiler, add it to
the function definition and prototype. Inlining is generally safer and
easier than writing an equivalent macro definition.
Nov 14 '05 #9

P: n/a

"Lars Langer" <ll*****@uwo.caREMOVETHIS> wrote in message
news:3_********************@news20.bellglobal.com. ..
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

Here's a guess as to the problem: was your original code accidently
different than the snippet you posted? I.e. was it originally:

if(endOfResponse = NULL) { // note single quote

Cordially,

Neil

Nov 14 '05 #10

P: n/a
In <ln************@nuthaus.mib.org> Keith Thompson <ks***@mib.org> writes:
Derrick Coetzee <dc****@moonflare.com> writes:
Keith Thompson wrote:
fputs(buffer, stdout);


Or this: puts(buffer);


Which is fine if you don't mind an extra newline at the end of the
output.


On the contrary, most of the time you want it! You need a very good
reason for using fputs or printf to generate a partial line of output.
Since it reduces the code readability, there must be some compelling
redeeming advantage in doing so (usually when a line is built from
small bits in a loop, and the newline is output at the end of the loop).

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #11

P: n/a
Da*****@cern.ch (Dan Pop) writes:
In <ln************@nuthaus.mib.org> Keith Thompson <ks***@mib.org> writes:
Derrick Coetzee <dc****@moonflare.com> writes:
Keith Thompson wrote:
fputs(buffer, stdout);

Or this: puts(buffer);


Which is fine if you don't mind an extra newline at the end of the
output.


On the contrary, most of the time you want it! You need a very good
reason for using fputs or printf to generate a partial line of output.
Since it reduces the code readability, there must be some compelling
redeeming advantage in doing so (usually when a line is built from
small bits in a loop, and the newline is output at the end of the loop).


How does that contradict what I wrote? puts(buffer) is fine if you
don't mind an extra newline at the end of the output.

The article to which I was responding used
printf(buffer);
where buffer was defined as
char buffer[8192];

My point was that using fputs() rather than printf() avoids problems
(undefined behavior) if the buffer happens to contain printf
directives.

We have no way of knowing whether buffer already ends in a newline,
but since it's declared to be 8192 bytes long, it's unlikely to
contain just a portion of a single line.

Your advice is sound for cases where the buffer is a relatively short
string with no trailing newline. The real point is that
fputs(buffer, stdout)
and
puts(buffer)
are not equivalent; which one is the best to use depends on the
circumstances.

--
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.
Nov 14 '05 #12

P: n/a
In <ln************@nuthaus.mib.org> Keith Thompson <ks***@mib.org> writes:
Da*****@cern.ch (Dan Pop) writes:
In <ln************@nuthaus.mib.org> Keith Thompson <ks***@mib.org> writes:
Derrick Coetzee <dc****@moonflare.com> writes:
Keith Thompson wrote:
> fputs(buffer, stdout);

Or this: puts(buffer);

Which is fine if you don't mind an extra newline at the end of the
output.


On the contrary, most of the time you want it! You need a very good
reason for using fputs or printf to generate a partial line of output.
Since it reduces the code readability, there must be some compelling
redeeming advantage in doing so (usually when a line is built from
small bits in a loop, and the newline is output at the end of the loop).


How does that contradict what I wrote? puts(buffer) is fine if you
don't mind an extra newline at the end of the output.


It merely points out that, most of the time, you *want* the newline,
so it's not a matter of "not minding it", but of needing it.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.