高分求教为什么我的VC执行的程序有问题

周靖峰 2012-02-22 09:22:51
昨天我问了个宽字符的问题,后来发现那段代码在大家的机器上都能正常运行,而在我的电脑上只能显示一部分,原先我以为是我的VC2005出了问题,于是今天卸了VC2005并换成了VC2008,但是令我奇怪的是今天在VC2008上运行的程序结果和昨天一样,还是只显示一部分

我很好奇到底是哪里出问题了,为什么大家的电脑都能正常运行,我的电脑就会运行出错呢

我把我的具体情况和大家说明一下:

我用的是盗版的VS2008,安装的时候我只安装了
Visual C++ Class & Template Libraries
Visual C++ Tools
Visual C++ Run-Time Libraries
这三个部分,其它东西都没有安装过

创建工程的时候,我建的都是空工程,然后在新工程中添加源文件,然后写代码,然后Build,最后运行

下面是我要运行的代码,我的运行结果是int,没有冒号和数字


#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

int main()
{
wchar_t buf[100];

swprintf(buf, L"%s:%d", L"int", 1024);
wprintf(L"%s", buf);
getch();
return 0;
}


求VC高手指教,帮我解决这个棘手的问题
...全文
220 26 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2012-02-23
  • 打赏
  • 举报
回复
[Quote=引用 25 楼 cxsjabcabc 的回复:]
楼主将unicode字符串和%s格式混用导致的不确定结果;

gcc4.2, 代码如下,需要稍微修改下,结果ok了:


C/C++ code


int main(void)
{
wchar_t buf[100];

swprintf(buf, 32, L"%S:%d", L"int", 1024);
wprintf(L"%S", buf……
[/Quote]
正解!
mk:@MSITStore:C:\MSDN98\98VS\2052\vccore.chm::/html/_crt_printf_type_field_characters.htm

printf Type Field Characters
The type character is the only required format field ; it appears after any optional format fields. The type character determines whether the associated argument is interpreted as a character, string, or number. The types C and S, and the behavior of c and s with printf functions, are Microsoft extensions and are not ANSI-compatible.
...
%s When used with printf functions, specifies a single-byte–character string; when used with wprintf functions, specifies a wide-character string. Characters are printed up to the first null character or until the precision value is reached.
%S When used with printf functions, specifies a wide-character string; when used with wprintf functions, specifies a single-byte–character string. Characters are printed up to the first null character or until the precision value is reached.
赵4老师 2012-02-22
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 nbda1121440 的回复:]
那啥,我发现了一个惊人的秘密,如果是C++文件的话,这段代码运行就正确了

为什么?为什么?为什么?

为什么是C文件代码运行就不正确了?????难道C语言不支持这么写?????

MSDN上关于swprintf的用法有点奇怪


C/C++ code

int sprintf(
char *buffer,
const char *format [,
……
[/Quote]
我将test.cpp改名为test.c后在VC2008下再现了楼主的错误。
估计是C编译器在编译楼主这句话的时候有Bug。
楼主可以试试提交此Bug到微软看能不能得到满意答复吧。
赵4老师 2012-02-22
  • 打赏
  • 举报
回复
推荐使用WinHex软件查看文件(包括源代码文件)或内存中的原始字节内容。
自信男孩 2012-02-22
  • 打赏
  • 举报
回复
会不会跟你的处理器的位数有关。
Aist-memory 2012-02-22
  • 打赏
  • 举报
回复
for the console application
面包大师 2012-02-22
  • 打赏
  • 举报
回复
直接去掉。。。有些编译器要包含这个
周靖峰 2012-02-22
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 focuslight 的回复:]

我的vs2008运行结果 int:1024

#include "stdafx.h"

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

int main()
{
wchar_t buf[100];

swprintf(buf, L"%s:%d", L"int", 1024);
……
[/Quote]
弱弱地问一下stdafx.h是什么东西,为什么我这里没有
Aist-memory 2012-02-22
  • 打赏
  • 举报
回复
我的vs2008运行结果 int:1024

#include "stdafx.h"

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

int main()
{
wchar_t buf[100];

swprintf(buf, L"%s:%d", L"int", 1024);
wprintf(L"%s", buf);
getch();
return 0;
}


/*
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
*/
程序员小迷 2012-02-22
  • 打赏
  • 举报
回复
楼主将unicode字符串和%s格式混用导致的不确定结果;

gcc4.2, 代码如下,需要稍微修改下,结果ok了:



int main(void)
{
wchar_t buf[100];

swprintf(buf, 32, L"%S:%d", L"int", 1024);
wprintf(L"%S", buf);


return 0;
}

周靖峰 2012-02-22
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 dd985081749 的回复:]

你用C++装C的数据进封装,封装好了再试试。
[/Quote]
这个怎么封装?
周靖峰 2012-02-22
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 shentujun 的回复:]

方案1:把wchar改成char版本试试
方案2:调用setlocale

还不行就是环境问题了
[/Quote]
对了,setlocle怎么用?可以举个例子吗?
dd985081749 2012-02-22
  • 打赏
  • 举报
回复
你用C++装C的数据进封装,封装好了再试试。
whc748227431 2012-02-22
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 shentujun 的回复:]
方案1:把wchar改成char版本试试
方案2:调用setlocale

还不行就是环境问题了
[/Quote]
+++
面包大师 2012-02-22
  • 打赏
  • 举报
回复
不是吧,应该是字符串。。。
周靖峰 2012-02-22
  • 打赏
  • 举报
回复
那啥,我发现了一个惊人的秘密,如果是C++文件的话,这段代码运行就正确了

为什么?为什么?为什么?

为什么是C文件代码运行就不正确了?????难道C语言不支持这么写?????

MSDN上关于swprintf的用法有点奇怪


int sprintf(
char *buffer,
const char *format [,
argument] ...
);
int _sprintf_l(
char *buffer,
const char *format,
locale_t locale [,
argument] ...
);
int swprintf(
wchar_t *buffer,
size_t count,
const wchar_t *format [,
argument]...
);
int _swprintf_l(
wchar_t *buffer,
size_t count,
const wchar_t *format,
locale_t locale [,
argument] ...
);
int __swprintf_l(
wchar_t *buffer,
const wchar_t *format,
locale_t locale [,
argument] ...
);
template <size_t size>
int sprintf(
char (&buffer)[size],
const char *format [,
argument] ...
); // C++ only
template <size_t size>
int _sprintf_l(
char (&buffer)[size],
const char *format,
locale_t locale [,
argument] ...
); // C++ only
template <size_t size>
int swprintf(
wchar_t (&buffer)[size],
size_t count,
const wchar_t *format [,
argument]...
); // C++ only
template <size_t size>
int _swprintf_l(
wchar_t (&buffer)[size],
size_t count,
const wchar_t *format,
locale_t locale [,
argument] ...
); // C++ only


难道swprintf的第二个参数不是字符串而是一个数字???

真的是这样吗??

这个数字应该写什么???
周靖峰 2012-02-22
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 echoyin59 的回复:]

你单步调试一下,看看buf里到底放的什么
[/Quote]
我单步看了一下,结果如图所示

SuperLy 2012-02-22
  • 打赏
  • 举报
回复
在vs2005下,执行一切正常;要么是你环境有问题,要么vc有问题...
建议看看是不是有什么特殊宏,把相关的东西重定义了...
周靖峰 2012-02-22
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 czh3642210 的回复:]
楼主用VS 2005,VS 2008, VS 2010之类的吧。。。
[/Quote]
是的,微软官方的VC2008 express貌似没有离线安装包,我只能用盗版的VS2008凑合着用了
周靖峰 2012-02-22
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 shentujun 的回复:]
方案1:把wchar改成char版本试试
方案2:调用setlocale

还不行就是环境问题了
[/Quote]
改为char可以,改为TCHAR也可以,但为什么用WCHAR就不行了呢,我非常奇怪
面包大师 2012-02-22
  • 打赏
  • 举报
回复
楼主用VS 2005,VS 2008, VS 2010之类的吧。。。
加载更多回复(3)

70,020

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧