有如下语句:
bool Exist()
{
unsigned int id = 123;
unsigned int time = 12315;
char pszBuf[1024] = {0};
sprintf(pszBuf, "select 1 from tab where id = %d and time = %d", id, time);
}在执行完sprintf语句后,
pszBuf中字符串为:select 1 from tab where id = 123 and time = 0正常情况下应该是:select 1 from tab where id = 123 and time = 12315吧,那位大侠遇到过这样的情况,
请不吝赐教,谢谢
bool Exist()
{
unsigned int id = 123;
unsigned int time = 12315;
char pszBuf[1024] = {0};
sprintf(pszBuf, "select 1 from tab where id = %d and time = %d", id, time);
}在执行完sprintf语句后,
pszBuf中字符串为:select 1 from tab where id = 123 and time = 0正常情况下应该是:select 1 from tab where id = 123 and time = 12315吧,那位大侠遇到过这样的情况,
请不吝赐教,谢谢
解决方案 »
- vc2008下编写WIN32程序乱码
- opengl 问题
- malloc最多能分配多少内存(VS6.0和VS2005得到的结果不同)
- 在线急,请高手指教
- "微软招聘出奇招",求解
- 如何利用coledispatchdriver使用ado
- 没有工作经验的人对VC的学习过程?
- cvcopy中掩膜是怎么发挥作用的
- 我对Windows API拦截技术经典之作Matt Pietrek的<<Windows 95 System Programming Secret>>第十章的研究
- 在VC下给一个DIALOG加个状态条,一般用什么办法?
- 求VC++使用“TCP/IP”和“UDP”这些协议的Demo
- Berkeley db 的问题
"select 1 from tab where id = 123 and time = 12315"
你的是什么环境,经常出现还是偶尔?
是程序走飞了,貌似是函数栈被破坏了
试着把相关语句防到函数之首(即帖子所示),还是存在同样问题,已经排除局部变量影响
调试的时候确认在语句sprintf(pszBuf, "select 1 from tab where id = %d and time = %d", id, time);
处,time == 12315同时发现一个有趣现象,在同样的位置,当把sprint语句改为:sprintf(sqlBuf, "select 1 from tab where time = %d and id = %d", time, id);
即,把id和time串行化顺序换一下,就能得到正常结果
环境VS2008
sprintf(sqlBuf, "select 1 from tab where time = %d and id = %d", time, id);
即把time串行化放在id前边,就能得到正常结果,
另外time是根据::time(NULL)获得在强制转化成UINT类型的一个数据,这个应该不会影响sprintf的结果吧
多线程程序,不过相关代码都是在主线程里跑,
切id和time都是通过函数参数传递的,且不是引用
time和id本都是函数参数传进来,sprintf位于在try模块中,出现上述问题后,
试着把相关语句防到函数之首(即帖子所示),还是存在同样问题,已经排除局部变量影响
应该是强制转换的问题::time(NULL)的返回值是time_t它的定义是
#ifdef _USE_32BIT_TIME_T
typedef __time32_t time_t; /* time value */
#else
typedef __time64_t time_t; /* time value */
而
UNIT 的定义是typedef unsigned int UINT;这样强制转换有截断数据的风险
修改如下
sprintf(sqlBuf, "select 1 from tab where time = %d and id = %ld",id,::time(NULL));
在语句sprintf(sqlBuf, "select 1 from tab where and id = %d time = %d",id, create_time);在执行sprintf之前已经有语句:unsigned int create_time = ::time(NUll);
即使强制截端,也应该只是数据问题,而在执行sprintf之后也不字符串后边也不应该是“time = 0”
吧,已经确认create_time为一个大于0的正数