[英]Pass a wxString into a printf-style varargs function
I am learning to code in C using K&R II. 我正在学习使用K&R II在C中进行编码。 I am tired of console apps and decided to get into a GUI environment. 我对控制台应用程序感到厌倦,因此决定进入GUI环境。 Decided to use Code Blocks and wxWidgets. 决定使用代码块和wxWidgets。 All is installed properly and working. 所有组件均已正确安装并且可以正常工作。 [Windows 7 x86, Code Blocks 13.12, wxWidgets 3.0.0] [Windows 7的86,代码块13.12,wxWidgets的3.0.0]
I am following the Tutorials on WxWidgets . 我正在关注WxWidgets教程 。 I am in Tutorial 9. I have it working, finally; 我在教程9中。 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. 我修改了我的应用程序,使它具有2个文本框和一个按钮,而输出的文本框和输入的一个组合框。
Visual C++ environment is totally foreign to me. Visual C ++环境是完全陌生。
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
. 对于按钮单击,我想阅读而不是打印"O brave new world!\\n"
,而是要阅读textbox1
输入的textbox1
并将其打印到textbox2
。
The instruction: 说明:
wxString Text = TextCtrl1->GetValue();
gets the string that has been entered in textbox1 获取已在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. 我需要知道如何/将...
参数更改为什么,以便它将数组形式的wxString wxString Text
传递给该printg函数。 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. 我确定我需要做的第一件事是将Text字符串更改为数组,或通过某种方式传递字符串本身。
UPDATE 01/08/13 3:35 PM 更新01/08/13下午3:35
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. 我从Textbox Enter事件中剪切了代码,并将其粘贴到ButtonClick事件中,现在我可以在文本框1到文本框2中获取文本了。
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. 现在,我需要一种方法将文本从文本框1传递到我的C文件之一,进行所有练习,然后将其传递回click事件,以传递给printg函数。
NOTE: I see confusion about printg. 注意:我对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. 我认为这是wxWidgets的一项功能,它使您可以打印回GUI窗体,而不是像printf一样打印到控制台。 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. 感谢您的答复。
Text.ToUTF8().data()
gives you const char *
Text.ToUTF8().data()
为您提供const char *
Also if you only want char*
instead of const char *
you can use const_cast<char *>(Text.ToUTF8().data())
另外,如果只希望使用char*
而不是const char *
,则可以使用const_cast<char *>(Text.ToUTF8().data())
The most convenient thing to do is to use wx equivalents of standard functions, ie 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. 最方便的做法是使用标准函数的wx等效项,在这种情况下,即wxPrintf()
,因为它们允许您传递wxString
(以及std::string
, std::wstring
, char*
和wchar_t*
)对象直接进行,不需要做任何特别的事情。 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. 使用printf()
或wxPrintf()
OTOH通常在GUI应用程序中不是很有用,您可能需要wxLogMessage()或类似的东西。
If you have a string: char *str = {"this is my string"};
如果您有一个字符串: char *str = {"this is my string"};
Then you can use printf() like this: 然后,您可以像这样使用printf() :
printf("%s\\n", str);
Note, there is no printg() in the C language. 注意,C语言中没有printg() 。 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. 只要每个参数都有一个格式说明符, printf()语句的原型就允许传递多个参数。 For example, this shows 3 format specifiers, and three arguments: 例如,这显示了3个格式说明符和3个参数:
printf("%s %d %f\n", str, 10, 13.5);
The "..." argument is called the ellipse argument. “ ...”自变量称为椭圆自变量。 It's covered in the K&R book, section 7.3 (in my edition anyway). 它在K&R书籍的第7.3节(无论如何是我的版本)中都有介绍。 It can have 0 or more arguments in it, as described by the *fmt argument. 如* fmt参数所述,它可以包含0个或多个参数。
If you already have a string ready, just call it like this: 如果您已经准备好字符串,则可以这样调用它:
printf("%s",str);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.