parsing using strstr

  • Thread starter Sean Bartholomew
  • Start date
S

Sean Bartholomew

im trying to parse my email file located in my system library on my
mac osx.
i have a form online that, when filled and submitted, is sent to my
email address.
i need to parse the fields that were filled out ignoring all the ip
and complicated stuff around it.
for eg. im trying to at least get the first block of text that ONLY
includes the fields and their values.

char *tempBuf;
ofstream testText ("/Volumes/iBook Docs/Documents/testText.txt");

char *start = strstr(buffer, "First Name=");
if (start != NULL)
{
strcpy(tempBuf, start);
char *stop = strstr(buffer, "Submit=");
if (stop != NULL)
{
tempBuf[stop - start] = '\0';
testText << tempBuf;
testText.close();
}
}

it DOES NOT WORK.
also, "stop - start" works out to a negative value, what gives???
"testText << start"...."testText << stop"....WORKS (prints a file
beginning from those exact positions) only if i comment out the
---"tempBuf[stop - start] = '\0';"---"
EVEN if i turn it around to... "start-stop"... so that the value is
positive.

once i do ANYTHING with "start" or "stop", cast them and copy them to
integers, or other char variables, ANYTHING, ..."testText <<
start"...PRINTS OUT AN EMPTY FILE.
but i need to find out the length between start and stop so that i
could insert a null character for the 1st record. once thats done i
could use strtok to parse the fields, which happen to be RETURN
delimited, but thats fine. i could deal with that. another thing
is...strstr DOESNT work with "\r".

and ANOTHER thing is, if i insert ...
testText << buffer;....
just before the ...testText << tempBuf;...it prints out a file
beginning with the start address... ie. from "First Name=", NOT FROM
THE BEGINNING OF THE EMAIL. so it means that buffer is being messed
with EVEN though i copied it to tempBuf and didnt touch it afterward.

please someone, tell me whats going on, my hair is long and its going
to be very short, VERY soon.
 
J

John Harrison

Sean Bartholomew said:
im trying to parse my email file located in my system library on my
mac osx.
i have a form online that, when filled and submitted, is sent to my
email address.
i need to parse the fields that were filled out ignoring all the ip
and complicated stuff around it.
for eg. im trying to at least get the first block of text that ONLY
includes the fields and their values.

char *tempBuf;
ofstream testText ("/Volumes/iBook Docs/Documents/testText.txt");

char *start = strstr(buffer, "First Name=");
if (start != NULL)
{
strcpy(tempBuf, start);
char *stop = strstr(buffer, "Submit=");
if (stop != NULL)
{
tempBuf[stop - start] = '\0';
testText << tempBuf;
testText.close();
}
}

it DOES NOT WORK.
also, "stop - start" works out to a negative value, what gives???
"testText << start"...."testText << stop"....WORKS (prints a file
beginning from those exact positions) only if i comment out the
---"tempBuf[stop - start] = '\0';"---"
EVEN if i turn it around to... "start-stop"... so that the value is
positive.

once i do ANYTHING with "start" or "stop", cast them and copy them to
integers, or other char variables, ANYTHING, ..."testText <<
start"...PRINTS OUT AN EMPTY FILE.
but i need to find out the length between start and stop so that i
could insert a null character for the 1st record. once thats done i
could use strtok to parse the fields, which happen to be RETURN
delimited, but thats fine. i could deal with that. another thing
is...strstr DOESNT work with "\r".

and ANOTHER thing is, if i insert ...
testText << buffer;....
just before the ...testText << tempBuf;...it prints out a file
beginning with the start address... ie. from "First Name=", NOT FROM
THE BEGINNING OF THE EMAIL. so it means that buffer is being messed
with EVEN though i copied it to tempBuf and didnt touch it afterward.

please someone, tell me whats going on, my hair is long and its going
to be very short, VERY soon.

Sounds like you are causing undefined behaviour by doing something with
pointers that you shouldn't, e.g. not initialising them, or not allocating
enough memory to hold all the data you are copying to them.

To know what is wrong however, we need to see a *complete* program. Snippets
of code doesn't cut it, because inevitably the mistake is in the code you
didn't post.

john
 
P

Peter Ammon

Sean said:
im trying to parse my email file located in my system library on my
mac osx.
i have a form online that, when filled and submitted, is sent to my
email address.
i need to parse the fields that were filled out ignoring all the ip
and complicated stuff around it.
for eg. im trying to at least get the first block of text that ONLY
includes the fields and their values.

char *tempBuf;
ofstream testText ("/Volumes/iBook Docs/Documents/testText.txt");

char *start = strstr(buffer, "First Name=");
if (start != NULL)
{
strcpy(tempBuf, start);

The above line breaks, because you have not allocated any memory for
tempBuf. Declare it as an array of char, or otherwise get it pointing
at some allocated memory, before you try to write into it.

[...]

-Peter
 
S

Sean Bartholomew

yes thanks so much that WAS the problem. it works now.


Peter Ammon said:
Sean said:
im trying to parse my email file located in my system library on my
mac osx.
i have a form online that, when filled and submitted, is sent to my
email address.
i need to parse the fields that were filled out ignoring all the ip
and complicated stuff around it.
for eg. im trying to at least get the first block of text that ONLY
includes the fields and their values.

char *tempBuf;
ofstream testText ("/Volumes/iBook Docs/Documents/testText.txt");

char *start = strstr(buffer, "First Name=");
if (start != NULL)
{
strcpy(tempBuf, start);

The above line breaks, because you have not allocated any memory for
tempBuf. Declare it as an array of char, or otherwise get it pointing
at some allocated memory, before you try to write into it.

[...]

-Peter
 

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

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top