Solved

C string function.

Posted on 2011-09-16
2
334 Views
Last Modified: 2012-05-12
Hi experts!

I need a little help with C string function. Look my code:

BSTR CCPlayCtrl::sErrMsgAdv(long lCard)
{
      CString strResult;
      LPCTSTR sPicFinal;
    BSTR msErrorAdv[16];

   msErrorAdv[lCard]=_T("File is invalid format”); //This works
    msErrorAdv[lCard]=_T("File" & sPicFinal & " is invalid format!"); //HERE PRODUCE ERROR, how to join these 3 texts? I may change type of BSTR msErrorAdv[16] but it should be able to full CString.

    strResult.Format( _T( "%s"),msErrorAdv[lCard]);
   return strResult.AllocSysString();

}
0
Comment
Question by:dvplayltd
2 Comments
 
LVL 31

Accepted Solution

by:
Zoppo earned 500 total points
ID: 36549538
Hi dvplayltd,

you cannot simply concatenate C-style strings (since they are just pointers to characters, not something string objects.

The first one is working because the string on the right side is even just a char pointer.

Easiest IMO would be to use CString instead of BSTR, i.e.:
      CString strResult;
   LPCTSTR sPicFinal; // this isn't initialized!!!
   CString msErrorAdv[16];

//   msErrorAdv[lCard]=_T("File is invalid format”); //This works
   msErrorAdv[lCard].Format( _T("File %s is invalid format!"), sPicFinal );

   strResult.Format( _T( "%s"),msErrorAdv[lCard]);
   return strResult.AllocSysString();

Open in new window

Further the code as you posted will produce nonsense since sPicFinal isn't initialized.

Hope that helps,

ZOPPO
0
 

Author Closing Comment

by:dvplayltd
ID: 36549647
10x. I put it in code, it work

P.S: Of course, I will init variable - I put it here just to be more clear. All my real use is much more complex, I post here code which is only relative to question,
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question