parsing using strstr

Discussion in 'C++' started by Sean Bartholomew, Jul 9, 2004.

  1. 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.
     
    Sean Bartholomew, Jul 9, 2004
    #1
    1. Advertising

  2. "Sean Bartholomew" <> wrote in message
    news:...
    > 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
     
    John Harrison, Jul 9, 2004
    #2
    1. Advertising

  3. Sean Bartholomew

    Peter Ammon Guest

    Sean Bartholomew wrote:

    > 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

    --
    Pull out a splinter to reply.
     
    Peter Ammon, Jul 9, 2004
    #3
  4. yes thanks so much that WAS the problem. it works now.


    Peter Ammon <> wrote in message news:<8BAHc.13067$>...
    > Sean Bartholomew wrote:
    >
    > > 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
     
    Sean Bartholomew, Jul 11, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joona I Palaste

    Re: memcmp versus strstr; reaction to chr(0)

    Joona I Palaste, Jul 24, 2003, in forum: C Programming
    Replies:
    0
    Views:
    427
    Joona I Palaste
    Jul 24, 2003
  2. Thomas Matthews

    Re: memcmp versus strstr; reaction to chr(0)

    Thomas Matthews, Jul 24, 2003, in forum: C Programming
    Replies:
    0
    Views:
    519
    Thomas Matthews
    Jul 24, 2003
  3. Burne C
    Replies:
    3
    Views:
    1,332
    Peter Ammon
    Jul 25, 2003
  4. Dan Pop
    Replies:
    0
    Views:
    380
    Dan Pop
    Jul 24, 2003
  5. Sean Bartholomew

    parsing using strstr

    Sean Bartholomew, Jul 8, 2004, in forum: C Programming
    Replies:
    2
    Views:
    587
    Richard Bos
    Jul 9, 2004
Loading...

Share This Page