可变参数及宽字符输出问题,为什么是乱码

xudacheng06 2012-01-04 09:54:28
代码如下
#include <iostream>
#include <cstdio>
#include <cstdarg>
#include <string>

void MLog(const wchar_t* log, ...)
{
va_list argList;
va_start(argList, log);
std::wstring logString = log;
logString = std::wstring(L"win: ") + log + std::wstring(L"\\n");
wprintf(logString.c_str(), argList);
va_end(argList);
}

int main()
{
MLog(L"%s,%d",L"HELLO WORLD",3);
}
...全文
292 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
平凡的思想者 2012-01-06
  • 打赏
  • 举报
回复
MLog函数写法有点问题,正确写法:

void MLog(const wchar_t* log, ...)
{
va_list argList;
va_start(argList, log);
WCHAR wBuffer[1024];
memset(wBuffer,0,sizeof(wBuffer));
_snwprintf( wBuffer, sizeof(wBuffer)/sizeof(WCHAR),log, argList);//把可变参数格式化输出到buffer
std::wstring logString = wBuffer;//关键是这行的问题
logString = std::wstring(L"win: ") + log + std::wstring(L"\\n");
wprintf(logString.c_str(), argList);
va_end(argList);
}
luciferisnotsatan 2012-01-04
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 xudacheng06 的回复:]

引用 1 楼 qq120848369 的回复:

应该用int vprintf(const char *format, va_list ap); 哦

应该是vwprintf吧,但是为什么不能用wprintf,应该也可以才对
[/Quote]
参数类型不一样
xudacheng06 2012-01-04
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 qq120848369 的回复:]

应该用int vprintf(const char *format, va_list ap); 哦
[/Quote]
应该是vwprintf吧,但是为什么不能用wprintf,应该也可以才对
qq120848369 2012-01-04
  • 打赏
  • 举报
回复
应该用int vprintf(const char *format, va_list ap); 哦
赵4老师 2012-01-04
  • 打赏
  • 举报
回复
对电脑而言没有乱码,只有二进制字节;对人脑才有乱码。
仅供参考
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
#include <windows.h>
#include <io.h>
#else
#include <unistd.h>
#include <sys/time.h>
#include <pthread.h>
#define CRITICAL_SECTION pthread_mutex_t
#define _vsnprintf vsnprintf
#endif
//Log{
#define MAXLOGSIZE 100000000
#define ARRSIZE(x) (sizeof(x)/sizeof(x[0]))
#include <time.h>
#include <sys/timeb.h>
#include <stdarg.h>
char logfilename1[]="MyLog1.log";
char logfilename2[]="MyLog2.log";
char logstr[16000];
char datestr[16];
char timestr[16];
char mss[4];
CRITICAL_SECTION cs_log;
FILE *flog;
#ifdef WIN32
void Lock(CRITICAL_SECTION *l) {
EnterCriticalSection(l);
}
void Unlock(CRITICAL_SECTION *l) {
LeaveCriticalSection(l);
}
#else
void Lock(CRITICAL_SECTION *l) {
pthread_mutex_lock(l);
}
void Unlock(CRITICAL_SECTION *l) {
pthread_mutex_unlock(l);
}
#endif
void LogV(const char *pszFmt,va_list argp) {
struct tm *now;
struct timeb tb;

if (NULL==pszFmt||0==pszFmt[0]) return;
if (-1==_vsnprintf(logstr,ARRSIZE(logstr),pszFmt,argp)) logstr[ARRSIZE(logstr)-1]=0;
ftime(&tb);
now=localtime(&tb.time);
sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);
sprintf(timestr,"%02d:%02d:%02d",now->tm_hour ,now->tm_min ,now->tm_sec );
sprintf(mss,"%03d",tb.millitm);
printf("%s %s.%s %s",datestr,timestr,mss,logstr);
flog=fopen(logfilename1,"a");
if (NULL!=flog) {
fprintf(flog,"%s %s.%s %s",datestr,timestr,mss,logstr);
if (ftell(flog)>MAXLOGSIZE) {
fclose(flog);
if (rename(logfilename1,logfilename2)) {
remove(logfilename2);
rename(logfilename1,logfilename2);
}
flog=fopen(logfilename1,"a");
if (NULL==flog) return;
}
fclose(flog);
}
}
void Log(const char *pszFmt,...) {
va_list argp;

Lock(&cs_log);
va_start(argp,pszFmt);
LogV(pszFmt,argp);
va_end(argp);
Unlock(&cs_log);
}
//Log}
int main(int argc,char * argv[]) {
int i;
#ifdef WIN32
InitializeCriticalSection(&cs_log);
#else
pthread_mutex_init(&cs_log,NULL);
#endif
for (i=0;i<10000;i++) {
Log("This is a Log %04d from FILE:%s LINE:%d\n",i, __FILE__, __LINE__);
}
#ifdef WIN32
DeleteCriticalSection(&cs_log);
#else
pthread_mutex_destroy(&cs_log);
#endif
return 0;
}
xudacheng06 2012-01-04
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 luciferisnotsatan 的回复:]

引用 2 楼 xudacheng06 的回复:

引用 1 楼 qq120848369 的回复:

应该用int vprintf(const char *format, va_list ap); 哦

应该是vwprintf吧,但是为什么不能用wprintf,应该也可以才对

参数类型不一样
[/Quote]
参数类型不一样根本不能编译,我贴的那段代码可以编译,只是乱码

64,639

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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