user3057562
user3057562

Reputation: 21

Pass a wxString into a printf-style varargs function

I am learning to code in C using K&R II. I am tired of console apps and decided to get into a GUI environment. Decided to use Code Blocks and wxWidgets. All is installed properly and working. [Windows 7 x86, Code Blocks 13.12, wxWidgets 3.0.0]

I am following the Tutorials on WxWidgets. I am in Tutorial 9. I have it working, finally; there are mistakes in the instructions.

I modified my app to have 2 text boxes and a button vs one text box for the output and one combo box for the input.

Visual C++ environment is totally foreign to me.

For the Button click I would like instead of printing, "O brave new world!\n", I would like to read what has been entered in textbox1 and print it into textbox2.

The instruction:

wxString Text = TextCtrl1->GetValue();

gets the string that has been entered in textbox1

I have a call to the function

void printg(char *fmt, ...);

I need to know how/what to change the ... argument to so it will passes the wxString Text in the form of an array, I think, to that printg function. I am sure the first thing I need to do is change the Text string to an array, or some way to pass the string itself.

UPDATE 01/08/13 3:35 PM

I cut the code from the Textbox Enter event and pasted it into the ButtonClick event and now I can get the text in Box one to box two.

Now, I need a way to pass the text from textbox 1 to one of my C files, do whatever the exercise is about and pass it back to the click event to be passed to the printg function.

NOTE: I see confusion about printg. I think it is a feature of wxWidgets that lets you print back to a GUI form rather than a console as printf does. It works.

I would put the code on here, but I do not know how. Tried before and get a message about it not being formatted properly.

Thanks for the replies.

Upvotes: 0

Views: 1822

Answers (4)

VZ.
VZ.

Reputation: 22688

The most convenient thing to do is to use wx equivalents of standard functions, i.e. wxPrintf() in this case, because they allow you to pass wxString (and also std::string, std::wstring, char* and wchar_t*) objects directly, without doing anything special. So you could simply write

wxString s = ...;
wxPrintf("My string is %s\n", s);

OTOH using either printf() or wxPrintf() is generally not very useful in GUI applications, you probably want wxLogMessage() or something similar.

Upvotes: 2

Nerf Herder
Nerf Herder

Reputation: 414

The "..." argument is called the ellipse argument. It's covered in the K&R book, section 7.3 (in my edition anyway). It can have 0 or more arguments in it, as described by the *fmt argument.

If you already have a string ready, just call it like this:

printf("%s",str);

Upvotes: 0

ryyker
ryyker

Reputation: 23226

If you have a string: char *str = {"this is my string"};

Then you can use printf() like this:

printf("%s\n", str);

Note, there is no printg() in the C language. And the prototype of the printf() statement allows for multiple arguments to be passed as long as there is a format specifier for each argument. For example, this shows 3 format specifiers, and three arguments:

printf("%s %d %f\n", str, 10, 13.5);

Upvotes: 0

Aniket Inge
Aniket Inge

Reputation: 25725

Text.ToUTF8().data() gives you const char *

Also if you only want char* instead of const char * you can use const_cast<char *>(Text.ToUTF8().data())

Upvotes: 3

Related Questions