使用sprintf格式化字符串并在MessageBox中显示它! [英] Formating a string using sprintf and showing it in MessageBox!

查看:106
本文介绍了使用sprintf格式化字符串并在MessageBox中显示它!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我想格式化一个字符串(使用sprintf)并把它放在一个消息框中

但是我尝试这样做,它不会似乎工作。

以下是我尝试做的示例示例:


char msg [120];

string my_name =" John";


sprintf(msg,我的名字是:%s",my_name);

MessageBox(NULL, msg,NULL,MB_OK);

这是我得到的错误:


错误C2664:''MessageBoxW'':无法从'转换参数2' 'char

[120]''''const unsigned short *''

指向的类型不相关;转换需要reinterpret_cast,

C风格的演员或函数风格的演员

我知道MessageBox想要一个LPCTSTR作为它的第二个参数。如果我用
代替(见下文)...


LPCTSTR msg;

string my_name =" John" ;


sprintf(msg,我的名字是:%s,my_name);

MessageBox(NULL,msg,NULL,MB_OK);

....我收到以下错误:


错误C2664:''sprintf'':无法从''const

unsigned short *''to''char *''

指向的类型不相关;转换需要reinterpret_cast,

C风格的演员或功能风格的演员


好​​像我尝试使用sprintf或者MessageBox不是

开心。如果我尝试在sprintf或MessageBox中投射''msg''我只是将b $ b得到垃圾作为我的输出。

有没有人有一个建议如何解决这个问题?请注意我

不想使用CString,因为我的平台似乎不支持

。我如何能够解决这个问题的示例代码将非常值得赞赏。


非常感谢您的帮助。

/ Babak

解决方案

嗨babak!

我想格式化一个字符串(使用sprintf)并放入它在消息框中
但是我尝试这样做,它似乎无法工作。
以下是我尝试做的示例示例:

char msg [120];
string my_name =" John";

sprintf(msg,我的名字是:%s",my_name);
MessageBox(NULL ,msg,NULL,MB_OK);

这是我得到的错误:

错误C2664:''MessageBoxW'':无法从''char <转换参数2 br /> [120]'''''const unsigned short *''




你编译一个UNICODE程序但是你正在处理ANSI字符串...


更改为


< code>

ifdef _UNIC ODE

#define tstring std :: wstring

#else

#define tstring std :: string

# endif


TCHAR消息[120];

tstring my_name = _T(" John");

__stprintf(msg, _T(我的名字是:%s),(LPCTSTR)my_name);

MessageBox(NULL,msg,NULL,MB_OK);

< /代码>


-

问候

Jochen


我关于Win32的博客和.NET
http://blog.kalmbachnet.de/


>错误C2664:''MessageBoxW'':无法将参数2从''char

[120]''转换为''const unsigned short *''
指向的类型不相关;转换需要reinterpret_cast,C风格的转换或函数式转换
看起来应用程序是Unicode。

LPCTSTR消息;
字符串my_name =" John" ; sprintf(msg,我的名字是:%s,my_name);
MessageBox(NULL,msg,NULL,MB_OK);
什么是字符串 ?

无论如何,试试:


TCHAR消息[120];

TCHAR * my_name = _T(" John") ;


_stprintf(msg,_T("我的名字是:%s"),my_name);

MessageBox(NULL,msg,NULL, MB_OK);


注:

----------------

LPCTSTR msg;

会使你的应用程序崩溃,因为它是一个非初始化的指针。

它扩展为

LPCTSTR:

LP =长指针(长期已经过时,从过去的时候起,

英特尔处理器使用分段存储器)

C = const

T =通用文本类型

STR =字符串

因此,这变为:

如果非Unicode:const char * msg;

如果是Unicode:const wchar_t * msg;

Generic:const TCHAR * msg;

------------- ---

注意_stprintf insted sprintf和_T(.....)

如果你读了sprintf的文档,你会看到一个表名为

" Generic-Text Rout ine映射。 TCHAR.H例程是你想要的b $ b。

----------------

了解一下Generic-Text Routine Mappings和Generic-Text Routine Mappings在MSDN。

----------------

阅读这篇文章: http://www.mihainita.net/20050306b.shtml

请注意我不知道我想使用CString,因为我的平台似乎不支持。



CString是MFC(VC.NET中的MFC / ATL)的一部分。如果您将

项目配置为使用MFC,那么您将获得它。


-

Mihai Nita [Microsoft MVP ,Windows - SDK]

------------------------------------- -----

将_year_替换为_以获取真实的电子邮件


>< code>

ifdef _UNICODE
#define tstring std :: wstring
#else
#define tstring std :: string
#endif

TCHAR消息[120] ;
tstring my_name = _T(" John");
__stprintf(msg,_T("我的名字是:%s"),(LPCTSTR)my_name);
MessageBox( NULL,msg,NULL,MB_OK);
< / code>



我没想到std :: string :-)

Jochen是对的,这就是你应该做的。


但他在__stprintf的双下划线中是对的:-)

纠正这个,你和你有一个运行的东西。


-

Mihai Nita [微软MVP,Windows - SDK]

----- -------------------------------------

用_ to替换_year_收到真正的电子邮件


Hi everyone
I want to format a string (using sprintf) and put it in a messagebox
but however I try to do it, it doesn''t seem to work.
Here is an example sample of what i try to do:

char msg[120];
string my_name = "John";

sprintf (msg, "My name is: %s ", my_name);
MessageBox(NULL, msg, NULL, MB_OK);
And this is the error I get:

error C2664: ''MessageBoxW'' : cannot convert parameter 2 from ''char
[120]'' to ''const unsigned short *''
Types pointed to are unrelated; conversion requires reinterpret_cast,
C-style cast or function-style cast

I know that MessageBox wants a LPCTSTR as its second parameter. If I
use that instead (see below)...

LPCTSTR msg;
string my_name = "John";

sprintf (msg, "My name is: %s ", my_name);
MessageBox(NULL, msg, NULL, MB_OK);
....I get the following error:

error C2664: ''sprintf'' : cannot convert parameter 1 from ''const
unsigned short *'' to ''char *''
Types pointed to are unrelated; conversion requires reinterpret_cast,
C-style cast or function-style cast

Seems like what ever I try to use either sprintf or MessageBox is not
happy. If I try to cast ''msg'' in either sprintf or MessageBox I just
get junk as my output.
Does anyone have a suggestion how I can solve this? Please note that I
don''t want to use CString since my platform does not seem to support
that. An example code of how I can solve this problem would be much
appreciated.

Thanks a lot for your help.
/Babak

解决方案

Hi babak!

I want to format a string (using sprintf) and put it in a messagebox
but however I try to do it, it doesn''t seem to work.
Here is an example sample of what i try to do:

char msg[120];
string my_name = "John";

sprintf (msg, "My name is: %s ", my_name);
MessageBox(NULL, msg, NULL, MB_OK);
And this is the error I get:

error C2664: ''MessageBoxW'' : cannot convert parameter 2 from ''char
[120]'' to ''const unsigned short *''



You compile a UNICODE program but you heare are dealing with ANSI strings...

Change it to

<code>
ifdef _UNICODE
#define tstring std::wstring
#else
#define tstring std::string
#endif

TCHAR msg[120];
tstring my_name = _T("John");
__stprintf(msg, _T("My name is: %s "), (LPCTSTR) my_name);
MessageBox(NULL, msg, NULL, MB_OK);
</code>

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/


> error C2664: ''MessageBoxW'' : cannot convert parameter 2 from ''char

[120]'' to ''const unsigned short *''
Types pointed to are unrelated; conversion requires reinterpret_cast,
C-style cast or function-style cast Looks like the applicaiton is Unicode.
LPCTSTR msg;
string my_name = "John";

sprintf (msg, "My name is: %s ", my_name);
MessageBox(NULL, msg, NULL, MB_OK); What is "string" ?
Anyway, try:

TCHAR msg[120];
TCHAR *my_name = _T("John");

_stprintf (msg, _T("My name is: %s "), my_name);
MessageBox(NULL, msg, NULL, MB_OK);

Notes:
----------------
LPCTSTR msg;
will crash you application, because it is a non-initialized pointer.
It expands to
LPCTSTR:
LP = Long Pointer (the long is obsolete, from the old days when
Intel procesors used segmented memory)
C = const
T = generic text type
STR = string
So, this becomes:
if Non-Unicode: const char * msg;
if Unicode: const wchar_t * msg;
Generic: const TCHAR * msg;
----------------
Note _stprintf insted of sprintf and _T(".....")
If you read the doc for sprintf, you will see a table named
"Generic-Text Routine Mappings". "TCHAR.H routine" is what you
allways want.
----------------
Read about the the "Generic-Text Routine Mappings" in MSDN.
----------------
Read this blurb: http://www.mihainita.net/20050306b.shtml
Please note that I don''t want to use CString since my platform does not
seem to support that.


CString is part of MFC (MFC/ATL in VC.NET). If you configure the
project to use MFC, then you will get it.

--
Mihai Nita [Microsoft MVP, Windows - SDK]
------------------------------------------
Replace _year_ with _ to get the real email


><code>

ifdef _UNICODE
#define tstring std::wstring
#else
#define tstring std::string
#endif

TCHAR msg[120];
tstring my_name = _T("John");
__stprintf(msg, _T("My name is: %s "), (LPCTSTR) my_name);
MessageBox(NULL, msg, NULL, MB_OK);
</code>


I did not think about std::string :-)
Jochen is right, that is what you should do.

But he is not right in double-underscore in __stprintf :-)
Correct that, and you have a running thing.

--
Mihai Nita [Microsoft MVP, Windows - SDK]
------------------------------------------
Replace _year_ with _ to get the real email


这篇关于使用sprintf格式化字符串并在MessageBox中显示它!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆