sprintf日期格式问题? [英] sprintf date format issues?

查看:109
本文介绍了sprintf日期格式问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我们在C ++层中有以下代码....我们使用此代码的DLL作为COM组件..但是下面抛出以下错误?有人请帮我这个吗?



代码:



sprintf(临时,%02d% 02d%04d,月,日,年);



这里是临时对象的声明:char * temp;



错误消息:



您的应用程序中出现未处理的异常。如果单击继续,应用程序将忽略此错误并尝试继续。如果单击退出,应用程序将立即关闭。



算术运算导致溢出。



你能告诉我可以用来解决问题的平等方法吗?

解决方案

你必须为临时变量的存储分配空间。在你的情况下,你应该将它声明为一个字符数组或执行动态内存分配(为终止null添加一个字符):



1)数组角色 -



char temp [9];



2)动态内存分配:



a)malloc - char * temp = malloc(9);

为避免使用temp完成内存泄漏,请使用:free(temp);



b)new - char * temp = new char [9];

为避免使用temp完成内存泄漏,请使用:delete []温度;

Hello ,

We have the following code in our C++ layer.... we are using the DLL of this code as a COM component.. but the below line of throwing the below error? Would someone please help me on this?

Code:

sprintf(temp,"%02d%02d%04d",month,day,year);

here is the declaratio of temp object: char * temp;

Error Message:

Unhandled exception has occred in your application.If you click continue, the application will ignore this error and attempt to continue. If you click Quit, the application will close immediately.

Arithmetic operation resulted in an overflow.

Would you please let me know the equalent mehtod i can use to resolve the issue.?

解决方案

You have to allocate space for the storage of the temp variable. In your case you should declare it either as an array of characters or do dynamic memory allocation to do this (add one character for the terminating null):

1) Array of characters -

char temp[9];

2) Dynamic memory allocation:

a) malloc - char *temp = malloc(9);
To avoid memory leaks when done with temp, use: free(temp);

b) new - char *temp = new char[9];
To avoid memory leaks when done with temp, use: delete[] temp;


这篇关于sprintf日期格式问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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