0

I am unable to convert the char * to CString.. the last 2 print lines print nothing...I need it as part of cstring to be able to pass it to a function..

char result[500];
strcpy(result, "\\\\.\\pipe\\");                    
strcat(result, a["BLOB"][i].GetString());// just appending to "result" a string I am reading from json file..
CString  temp(result);
printf("\n1-The pipeName we are looking at is %s \n", result);
printf("\n2-The pipeName we are looking at is %s \n", temp);
printf("\n3-The pipeName we are looking at is %s \n", CString(result,500));
7
  • What is CString and where does it come from? It's not part of standard C++. Commented Oct 14, 2020 at 18:15
  • Between CString and .GetString(), I am assuming this is referring to WinAPI, and have added the tag... a complete example (with includes) would have clarified that. Commented Oct 14, 2020 at 18:17
  • @DevSolar It is probably this, which is in mfc, rather than winapi. Commented Oct 14, 2020 at 18:19
  • @TanveerBadar: OK... I come from the Amiga and went from there directly to Linux, so everything from Microsoft is "WinAPI" to me. :-D Feel free to re-tag. Commented Oct 14, 2020 at 18:26
  • 1
    @ThomasMatthews: It's the MFC's (Thanks, Tanveer) C++ string type, and has been well before the monstrosity that is C++/CLI ever saw the light of day. Commented Oct 15, 2020 at 9:07

3 Answers 3

2

How would printf() know what CString is? What a class is? How to convert CString to a c-style string?

This is working as expected.

What you are looking for is probably documented here, based on the assumption that MFC/WFC also provide a CString.

Sign up to request clarification or add additional context in comments.

Comments

2

printf is crude. Using %s with printf will assume that the first pointer-size amount of bytes in whatever you pass to it are actually a pointer to a string (aka char*).

That may or may not be true for CString. In your case it seems that it isn't.

To use printf you need to do it more like:

printf("\n2-The pipeName we are looking at is %s \n", temp.GetString());

Or else just stop using printf and use something with more type-safety, such as cout

Comments

2

Build the string directly into the CString, and use the builtin cast to const char * when printing.

CString  temp("\\\\.\\pipe\\");
temp += a["BLOB"][i].GetString(); // or: temp.Format("\\\\.\\pipe\\%s", a["BLOB"][i].GetString());
printf("-The pipeName we are looking at is %s\n", (const char *)temp);

2 Comments

The format arguments we are passing are incorrect.. any suggestions?
@ccoding What's the exact line of code the error points to? Also, you don't say what type a["BLOB"][i].GetString() returns, but if it's not a const char * then you probably need to add a cast to that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.