Hi friends,
I have a problem where I am reading a string value from data file through my Record Set into basic string and then trying to convert it into LPCSTR or say LPSTR and where I am getting into trouble.

The record set that i used is as follows:

struct RecordSet
{
TCHAR *Value;
TCHAR *FieldName;
};

The sample code that I used in my program is as below:

RecordSet * rst;
m_ptDBRecords->MoveFirst();
rst = m_ptDBRecords->GetActiveRecordSet();

if(rst != NULL)
{
sJobNumber = string(rst[0].Value); // sJobNumber is std::string
psJobNumber = sJobNumber.c_str(); // psJobNumber is LPCSTR
return true;
}

If you see the first line inside the curly braces, i am converting Value read from recordset into basic string and storing it into sJobNumber.
In the second line the RHS of equal sign statement converts basic string into const char* using c_str() then store the value into psJobNumber. Actually as per the code it has to give me the final value in psJobNumber, but it is coming out of the brace when control reaches the second line.

Please give me information about the followings:
1. Conversion of basic string to LPSTR
2. Conversion of basic string to LPCSTR
3. Conversion of TCHAR* to LPSTR or LPCSTR

Your help will be greately appriciated.

Many Thanks,
Ishwar

the pointer returned by string::c_str() is
a. a const char*. if you need a LPSTR (char*), you need to copy (strcpy) it to an array of chars
b. usable as a LPCSTR (const char*) only as long as you have not modified the string. you need to make your own copy if you want to use it for ever.

also, in your code TCHAR can be either a char or a wchar_t. if it is a char, a TCHAR* is already an LPSTR. if it is a wchar_t, a TCHAR* would be an LPWSTR. you need to convert from wide to narrow characters to get an LPSTR

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.