Searching for values in a string and getting a picture

This is a discussion on Searching for values in a string and getting a picture within the C++ Programming forums, part of the General Programming Boards category; I am making this program using Borland C++ Builder 4 (yes old i know but cant afford to get the ...

  1. #1
    Se7en
    Guest

    Searching for values in a string and getting a picture

    I am making this program using Borland C++ Builder 4 (yes old i know but cant afford to get the newer version) anyway what my program does is send a post data to a webpage. It then retrieves the post date and stores the whole code of the webpage to the variable 'htmlcode'

    now part of the html has this code


    <INPUT TYPE=HIDDEN NAME=cdate VALUE="6400">

    what I want to do is get the value of 6400 (it changes all the time)

    I have tried the string operation which on the helpfile said should be
    variablename.find();

    but .find does not seem to work (I already add #include <string.h>)

    Can anyone enlighten me on how to isolate '6400' so I can store it in another variable?

    Also the page I get after I send the post data contains a picture, I havent seem to find a way to get this picture since it is a .cgi file and If I load it using the HTML1 module i get a cgi error (since it needs the post data) I have no Idea how to get this file atm.

    My code as it stands right now is

    {
    AnsiString postdata;
    AnsiString htmlcode;
    postdata = "checkcode=" + txtCheckcode->Text +
    "&imagenumber=" + txtimagenumber->Text +;
    NMHTTP1->TimeOut = 5000;
    NMHTTP1->InputFileMode = false;
    NMHTTP1->OutputFileMode = false;
    NMHTTP1->ReportLevel = Status_Basic;
    if (CheckBox1->Checked)
    {
    NMHTTP1->Proxy = cmbProxy->Text;
    NMHTTP1->ProxyPort = StrToInt(cmbProxyport->Text);
    }
    NMHTTP1->HeaderInfo->Referer = "http://localhost/imagegallery.htm";
    NMHTTP1->Post("http://localhost/getdata.cgi", postdata);
    memDisp->Text = NMHTTP1->Body;
    memDisp->Text = htmlcode;

    }

  2. #2
    Se7en
    Guest
    oh sorry forgot about the code tag

    Code:
    {
    AnsiString postdata;
    AnsiString htmlcode;
            postdata  = "checkcode=" + txtCheckcode->Text +
                      "&imagenumber=" + txtimagenumber->Text +;
      NMHTTP1->TimeOut = 5000;
      NMHTTP1->InputFileMode = false;
      NMHTTP1->OutputFileMode = false;
      NMHTTP1->ReportLevel = Status_Basic;
      if (CheckBox1->Checked)
      {
      NMHTTP1->Proxy = cmbProxy->Text;
      NMHTTP1->ProxyPort = StrToInt(cmbProxyport->Text);
      }
      NMHTTP1->HeaderInfo->Referer = "http://localhost/";
      NMHTTP1->Post("http://localhost/getdata.cgi", postdata);
      memDisp->Text = NMHTTP1->Body;
      memDisp->Text = htmlcode;
    
    }

  3. #3
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>> but cant afford to get the newer version

    There are a number of free compilers and IDE's available. The Borland 5.5 compiler, (command line), is a damn fine compiler. Many people here use the Dev C++ over the mingw compiler. Get Dev C++ here, or Borland here

    Is the value the only thing that is in quotes in your string? If so, Go to the first character after a quote then read the digits until you get to the second quote character.

    Code:
    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    int main()
    {
        char String[]  = "<INPUT TYPE=HIDDEN NAME=cdate VALUE=\"6400\">";
        char ValueBuf[10];
        char *p;
        int i = 0;
        int Value;
    
        p = strstr(String, "\"");
        ++p;
        while (*p != '"')
        {
            ValueBuf[i] = *p;
            ++i;
            ++p;
        }
        Value = atoi(ValueBuf);
        printf("Value is : %d\n", Value);
    
        return 1;
    }
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  4. #4
    Se7en
    Guest
    Unfortuneately it is one of the many, and is also not the first one too

    are there others ways I can use?

  5. #5
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Okay, so is it the only field of digits? If so, you can scan the string looking for digits. If not, is there only one "VALUE=" tag, if so, use strstr() like I did before to look for the tag, then proceed as I did before.

    If none of these are true, perhaps you need to further define your problem. You need to establish a way of unambiguously identifying the correct field, if you can't do it, nor can the computer!
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  6. #6
    Se7en
    Guest
    Thanks for the help, Im new at this my first program thats not from a tutorial really and first that has to do with a webpage.
    anyway the string is an Ansi string and it contains the whole source code which is something like this

    <HTML>
    :
    some text here
    :
    <FORM method=post action=getdata.cgi>
    <INPUT TYPE=HIDDEN NAME=cdate VALUE="6400">
    <INPUT TYPE=HIDDEN NAME=username VALUE="testing">
    <INPUT TYPE=HIDDEN NAME=Password VALUE="testing">
    <INPUT TYPE=HIDDEN NAME=Email VALUE="test@testing.com">
    <INPUT TYPE=HIDDEN NAME=IMAGENUMBER VALUE="9">
    <INPUT TYPE=HIDDEN NAME=CRC VALUE="4923.07409">
    :
    more texts here
    :
    </HTML


    I left out some of the things to keep it short but theres info in there like the Username, Picture and other details involved.

    The values of CDATE, CRC and IMAGENUMBER all change and those are the values that I would like to get and store them all into different variables.

    In the example I used cdate to keep it simple and decided I could work on the rest after I get an example of how to do it

    I feel bad asking these questions without being a member, Ill register now ^_^

  7. #7
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Is the formatting always like that? What I mean here is...

    >>> <INPUT TYPE=HIDDEN NAME=cdate VALUE="6400">

    ... is cdate always followed by a space, then the string VALUE=", then the number, then another double quote? There are ways to do it anyway, but if the format is absolutely fixed, then we don't need to modify the earlier code that much to make it work, simply use strstr() to look for "cdate VALUE=\"" instead of just "VALUE=\"" - if you see what I mean.

    If the format varies, there are ways around it but it gets more fiddly.
    Code:
    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <windows.h>
    
    int main()
    {
        char String[]  = "<INPUT TYPE=HIDDEN NAME=cdate VALUE=\"6400\">";
        char ValueBuf[10];
        char *p;
        int i = 0;
        int Value;
    
        p = strstr(String, "cdate VALUE=\"");
        p += 13;
        while (*p != '"')
        {
            ValueBuf[i] = *p;
            ++i;
            ++p;
        }
        Value = atoi(ValueBuf);
        printf("Value is : %d\n", Value);
    
        return 1;
    }
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  8. #8
    Registered User
    Join Date
    Jun 2003
    Posts
    1
    Hey thanks alot man Really helped!

    there were some problems tho, specifically strstr(); does not work with ansistring so after looking around the help files i found pos();

    and here is the final code as of right now, for anyone that might run into this problem

    Code:
    //getcdate start
      Value = htmlcode.Pos("cdate VALUE=\"");
    
      Value = Value + 13;
      i = 0;
      while (htmlcode[Value] != '"')
       {
           ValueBuf[i] = htmlcode[Value];
           Value++;
           i++;
       }
        cdateValue = ValueBuf;
    //getcdate end
    thanks alot again ^_^
    Last edited by Se7en; 06-24-2003 at 04:47 AM.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21