void CRobotDlg::OnBnClickedSnip()
{
    //---------- navigate to Snip
    CString sUrlSnip= L"http://Snip";
    m_ctlBrowser.Navigate(sUrlSnip, 0,0,0,0); 

        while (m_ctlBrowser.get_ReadyState() != READYSTATE_COMPLETE) {
        DelayMs( 100 );
    }
    //------ page is ready to process
    IHTMLDocument2* pDoc= (IHTMLDocument2*)m_ctlBrowser.get_Document();
    IHTMLElement* pElemBody= NULL;
    pDoc->get_body(&pElemBody);

    CComBSTR bstrHTML;
    pElemBody->get_innerHTML(&bstrHTML);
    CString sBody= bstrHTML;  // easier to work with as a CString
    //MessageBox(sBody);  // debug, shows the string as a message
    //----------------- zero-in on the data of interest
    int nOffset= sBody.Find(L"last href");
    CString sTmp= sBody.Mid(nOffset); // lop off the preceding HTML
    //MessageBox(sTmp);  // debug, shows the string as a message

    int nStart= sTmp.Find(L"Snip")+N;  // isolate last page number
    int nEnd=   sTmp.Find(L"Snip")-N;
    CString sLastPageNumber= sTmp.Mid(nStart, nEnd-nStart);

    //MessageBox(sTmp);  // debug, shows the string as a message
    MessageBox(sLastPageNumber);  // debug, shows the most recently assigned data   
}

Hello, I have used this code to successfully harvest a string holding some data I need (a number, going to be the iterator in a for loop). I now need to convert this string, which is definately holding an integer.

I have hit a brick wall so to speak, I will detail what i've tried, all either breaking my program, or refusing to compile.

CStringA sLastPageNumber= sTmp.Mid(nStart, nEnd-nStart);

This would work, but it would mean going all the way back up the method, replacing CString with CStringA, which doesnt work with stuff like "&bstrHTML". I just tried this after I saw CStringA on some barely related post I rolled into. I have no idea what the differences are.

    // Convert a TCHAR string to a LPCSTR
    CT2CA ConvertedLastPageNumber (LastPageNumber);

    // construct a std::string using the LPCSTR input
    std::string strStd (ConvertedLastPageNumber);

This would work if my compiler wasnt throwing these errors;
error C2039: 'string' : is not a member of 'std'
error C2065: 'string' : undeclared identifier
error C2146: syntax error : missing ';' before identifier 'strStd'
error C3861: 'strStd': identifier not found
Ref for this solution

CString s = "123";
int x = atoi( s );

Annoyingly, this implies it should work right out of the box, it throws this error message though; error C2664: 'atoi' : cannot convert parameter 1 from 'CString' to 'const char *' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called.

It's complaining about converting from a 'const char[4]'into an ATL::CStringT<wchar_t,StrTraitMFC_DLL...etc

I'm using MFC with a Unicode build, I cant change it to multi-byte, I tried and it broke the program.

I'm really lost here, little help please.

Recommended Answers

All 6 Replies

use CString's GetBuffer() method, similar to std::string's c_str()

int x = atoi(s.GetBuffer());

int x = atoi(sLastPageNumber.GetBuffer());

error C2664: 'atoi' : cannot convert parameter 1 from 'wchar_t *' to 'const char *'
Types pointed to are unrelated; conversion requires reinterpret_cast,C-style cast or function-style cast.

#include "stdafx.h"
#include "SnipRobot.h"
#include "SnipRobotDlg.h"
#include "afxdialogex.h"
#include <mshtml.h> 
#include <atlbase.h>
#include <comdef.h>

These are the headers I'm using if that's any help.

I see you are compiling in UNICODE. Use wtoi() instead of atoi() See this link, or if you want your program to be portable between UNICODE and ascii you can use _tstoi() which is defined in tchar.h

Thankyou very much, it worked. Although unfortunately I am putting this program on a backburner, it seems that every time I add a new part to the program I hit a wall.

Although unfortunately I am putting this program on a backburner, it seems that every time I add a new part to the program I hit a wall.

That's part of the learning process -- don't give up just because something is confusing, do some research and ask questions here at DaniWeb.

Thanks Dragon, I just am beginning to feel that other people are writing my program for me, right after you helped me convert that string to an int it was awesome, I really felt like I could access information and act upon it. Then immediateley afterwards I tried to print that int in a messagebox and it wouldnt work... so eventually shrugged it off, feeling pretty sure that it is the correct data (in this case it was a string of '28' becoming and integer of 28, it really should have been fine), so I built the for loop, the whole point of finding my iterator in that mess of HTML, it ran once and ended. Apart from that I have a problem with the HTML I'm accessing(scraping), the people that wrote the site re-used element names, which I wasnt expecting, it took me a while to realise my program crashing was because my code was trying to 'tick' images(!), with the same ID(name in this case)as the Checkboxes I need, further up the code. So with that I need to rewrite my ElemFromId function, which is borrowed code I barely understand, to remove the offending (it's basically a useless header) part before it searches for the ID's I'm interested in particular.

So, you answering one question created 3 more!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.