Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

copying std::string to CString

Status
Not open for further replies.

bkelly13

Programmer
Aug 31, 2006
98
US
I am having a difficult time moving data from std::string to a CString that is part of a structure. Here is the essence:
Code:
typedef struct
{
   int		stream_number;
   CString	sfid_name;
   int		sifd_counter;
   CString	msg_prefix;
   int		length;
}  td_input_stream;
td_input_stream *p_stream_struct;
p_stream_struct = new( td_input_stream );
status = Get_Next_Definition( &expected, &t_string );
if( status )
{
   CString sfid_name( t_string.c_str());  // test code, is ok
   p_stream_struct->sfid_name( t_string.c_str() ); // desired code, NOT ok
   p_stream_struct->sfid_name = sfid_name; 
}
The error for the NOT ok line of code is:
error C2064: term does not evaluate to a function taking 1 arguments
There is a clue that I don't know how to interpert. Upon hovering the cursor over sfid_name in the first line of test code
the displayed hint is: CString sfid_name
Upon hovering over the ->sfid_name the hint is: CString __unnamed_00a8_1::sfid_name
What do I need to change?




We need to know what a dragon is
before we study its anatomy.
(Bryan Kelly, 2010)
 
And as long as std::strings are on the menu, if I may be permitted: std::string named one_line contains:
In Stream Begin
with no leading or trailing spaces. The code contains:
Code:
descriptor = one_line.substr( 0, location );
if( descriptor.compare( "In Stream Begin" ) )[ ... ] ...
But the if statement does not reflect a true condition. Why not?


We need to know what a dragon is
before we study its anatomy.
(Bryan Kelly, 2010)
 
std::string::compare returns int value (0 on is equal to).
Code:
if (descriptor.compare("In Stream Begin")) [ // is NOT equal!

The OP:
May be you want
Code:
p_stream_struct->sfid_name = t_string.c_str();
?
Syntactically you try to call sfid_name as a function or a pointer to function.
 
Your reply was helpful. I am almost there but some troubles remain. The application is in Visual Studio 2008, MFC, No ATL, No CLR. I have some code to read text from a file. That code uses reads the text into an std::string. When an error is discovered I need to show it to a user with afxMessageBox(). After a bunch of permutations the code looks something like:
Code:
bool Get_Stuff( std::string *data_type, int *value )
{
...
// error display stuff
std::string temp2 = *data_type;
CString temp2( temp1.c_str() );
CString a_string;
a_string.Format( L"expected text is %s and more info", temp2 );
AfxMessageBox( a_string );
...
return status;

I am trying to get the string from *data_type into my error message for the user. The debugger shows that *data_type contains valid text. What must I do to get it in the message box?

We need to know what a dragon is
before we study its anatomy.
(Bryan Kelly, 2010)
 
Try this:
Code:
a_string.Format(L"... %s ...", (LPCTSTR)temp2);
May be it helps...

Thank God, I don't use MFC ;)
 
For the record, I think I finally have a handle on this concept. (Please don't hammer me for typos and syntax, my code is at work and I cannot cut and paste.)
Code:
std::a_string = "a_string";
std::string *b_string = "b_string";

  // does not work
CString c_string.Format(L"a is %s b is %s", <and any form of cast or translation>);

CString d_string( a_string.c_str() );
CString e_string( b_string->c_str() );
CString f_string;  // don't format it yet, just declare

f_string.Format( L"a is %s b is %s", d_string, e_string );
When I need the contents of a std::string, or even a _bstr_t, in a CString, I must move it into a CString (completely, all by its self), then I can format into the target CString with the .Format() method.
I need to do this because the vendor starter application uses CString in the MFC project, and uses _bstr_t for calls to their application, and finally, the std function to read text files uses std::string.
I have had cases where trying to declare f_string (above) and formatting it in the same line of code just does not work while using two line of code does work. I can now make it all work so I won't spend much time sweating it. (All the places I need this are in the user part of the applications and time is not a problem. The real time part does not need all these gyrations.)

Thank you again for all your responses.

We need to know what a dragon is
before we study its anatomy.
(Bryan Kelly, 2010)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top