Solved

C string function.

Posted on 2011-09-16
2
332 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 30

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

707 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now