473,395 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

strstr(char *str1, char *str2)

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
12 3281
"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
Keith Thompson wrote:
fputs(buffer, stdout);


Or this: puts(buffer);
--
Derrick Coetzee
Nov 14 '05 #3
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
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
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
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
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
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

"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Paul Emmons | last post by:
In writing a function similar to strstr(), I'm calling both of the arguments "const char *". My compiler (gcc) complains "warning: return discards qualifiers from pointer target type" unless I...
6
by: dddddddd2444444 | last post by:
Hi,please help... It works fine when I define a 2-D array like char code. But it won't work when I try to define the array dynamically using a function. It just crashes. Does anyone know why?...
24
by: John Smith | last post by:
How to test whether strstr() returns a null pointer or not? Do I use something like the following? if(NULL != strstr(str1,str2)) That does not seem to work.
4
by: smnoff | last post by:
I am trying to use the strstr function but it doesn't seem to work with a dynamically allocated string that I pass into a function. Specifically, I am using a Macromedia C-level extensibility and...
6
by: linq936 | last post by:
Hi, I have the following code: #include <stdio.h> int main(void){ char* str1 = "abc"; char* str2 = '\0'; if ( strstr(str1, str2) == NULL ){ printf("yes\n");
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.