pampa_analytica
asked on
Conversion from LPTSTR* to STL string
Hi,
I would like to know how to convert the string from LPTSTR* to STL string object.
Sample code would be of great help.
Thanks in advance.
nt WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
LPTSTR *szArglist;
int nArgs;
szArglist = CommandLineToArgvW(GetComm andLineW() , &nArgs);
//I would like to convert the szArglist to STL string object
}
Regards,
Pampa
I would like to know how to convert the string from LPTSTR* to STL string object.
Sample code would be of great help.
Thanks in advance.
nt WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
LPTSTR *szArglist;
int nArgs;
szArglist = CommandLineToArgvW(GetComm
//I would like to convert the szArglist to STL string object
}
Regards,
Pampa
That's the way to do it. It does work. "string" does have a constructor that takes a char *.
ASKER
I tried in the way what Amit has posted, but it is not working. It throws me error:
f:\Viz\Viz_src\DataVizGUI\ DataVizGUI .cpp(36): error C2664: 'std::basic_string<_Elem,_ Traits,_Ax >::basic_s tring(cons t std::basic_string<_Elem,_T raits,_Ax> ::_Alloc &)' : cannot convert parameter 1 from 'LPTSTR * ' to 'const std::basic_string<_Elem,_T raits,_Ax> ::_Alloc &'
with
[
_Elem=char,
_Traits=std::char_traits<c har>,
_Ax=std::allocator<char>
]
and
[
_Elem=char,
_Traits=std::char_traits<c har>,
_Ax=std::allocator<char>
]
Please provide me some sample code to convert the char*/LPTSTR* to STL string.
Thanks in advance,
Pampa
f:\Viz\Viz_src\DataVizGUI\
with
[
_Elem=char,
_Traits=std::char_traits<c
_Ax=std::allocator<char>
]
and
[
_Elem=char,
_Traits=std::char_traits<c
_Ax=std::allocator<char>
]
Please provide me some sample code to convert the char*/LPTSTR* to STL string.
Thanks in advance,
Pampa
This works for me:
LPTSTR lptstr = "this is the lptstr";
string the_string(lptstr);
cout << the_string << endl;
LPTSTR lptstr = "this is the lptstr";
string the_string(lptstr);
cout << the_string << endl;
If this still does not help, show your source code.
Try this:
for(int i=0;i<nArgs;i++)
{
if(!i)
the_string +=' ';
the_string += szArglist[i];
}
if you want all arguments to be palced in a string.
for(int i=0;i<nArgs;i++)
{
if(!i)
the_string +=' ';
the_string += szArglist[i];
}
if you want all arguments to be palced in a string.
...my mistake, if(!i) should be if(i)
> string stlStr ( szArglist ) ;
LPTSTR * is not the same as LPTSTR. The first is an array of pointers, the second is one pointer.
Since szArglist is an array of pointers, you would need to do
string stlStr1(szArglist[0]);
string stlStr2(szArglist[1]);
etc.
LPTSTR * is not the same as LPTSTR. The first is an array of pointers, the second is one pointer.
Since szArglist is an array of pointers, you would need to do
string stlStr1(szArglist[0]);
string stlStr2(szArglist[1]);
etc.
If you are using unicode, use wstring instead of string
wayside is absolutely correct
All of us didn't notice that ur szArglist is a pointer of type LPTSTR
Amit
All of us didn't notice that ur szArglist is a pointer of type LPTSTR
Amit
I did ;)
Sorry nonubik,
I should have had a meticulous look at your code
Amit
I should have had a meticulous look at your code
Amit
np, maybe it was too obscure.
ASKER
Hi guys,
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
LPTSTR *szArglist;
int nArgs;
szArglist = CommandLineToArgvW(GetComm andLineW() , &nArgs);
string the_string;
//I would like to convert the szArglist to STL string object. I tried this:
for( i=1; i<=nArgs-1; i++)
{
the_string = the_string + szArglist[i];
MessageBox(NULL,szArglist[ i],0,0);
}
}
But i got this error:
f:\Viz\Viz_src\DataVizGUI\ DataVizGUI .cpp(63): error C2678: binary '+' : no operator found which takes a left-hand operand of type 'std::string' (or there is no acceptable conversion).
I am unable to solve this coz I am pretty new to VC++. PLease help me out in solving this problem.
Thanks in advance,
Pampa
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
LPTSTR *szArglist;
int nArgs;
szArglist = CommandLineToArgvW(GetComm
string the_string;
//I would like to convert the szArglist to STL string object. I tried this:
for( i=1; i<=nArgs-1; i++)
{
the_string = the_string + szArglist[i];
MessageBox(NULL,szArglist[
}
}
But i got this error:
f:\Viz\Viz_src\DataVizGUI\
I am unable to solve this coz I am pretty new to VC++. PLease help me out in solving this problem.
Thanks in advance,
Pampa
pampa_analytica,
std::string does have a binary + operator
and the above code works fine for me
Amit
std::string does have a binary + operator
and the above code works fine for me
Amit
ASKER
I got this error while I was trying to do this:
for( i=1; i<=nArgs-1; i++)
{
the_string += szArglist[i];
MessageBox(NULL,szArglist[ i],0,0);
}
f:\Viz\Viz_src\DataVizGUI\ DataVizGUI .cpp(64): error C2679: binary '+=' : no operator found which takes a right-hand operand of type 'LPTSTR' (or there is no acceptable conversion)
for( i=1; i<=nArgs-1; i++)
{
the_string += szArglist[i];
MessageBox(NULL,szArglist[
}
f:\Viz\Viz_src\DataVizGUI\
ASKER
#include <string>
using namespace std;
These are my includes in the project
using namespace std;
These are my includes in the project
I think u are using VC++6.0
But I tried on it & it works fine for me
Amit
But I tried on it & it works fine for me
Amit
ASKER
No Amit I am using VC++7.0. I tried but still Iam getting the same error.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
try a cast:
the_string += (char *)szArglist[i];
the_string += (char *)szArglist[i];
And LPSTR is actually a pointer to a char string
Hence this should work (I haven't tried it)
string stlStr ( szArglist ) ;
Amit