strstr(char *str1, char *str2)

L

Lars Langer

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
 
K

Keith Thompson

Lars Langer said:
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);
 
B

Barry Schwarz

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

Keith Thompson

Keith Thompson said:
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 ...".
 
M

Martin Ambuhl

Lars said:
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."
 
L

Lars Langer

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 said:
Lars said:
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."
 
M

Martin Ambuhl

Lars said:
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

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

Neil Ferguson

Lars Langer said:
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
 
D

Dan Pop

In said:
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
 
K

Keith Thompson

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

Dan Pop

In said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top