sprintf导致segmantation错误 [英] sprintf causing segmantation fault

查看:106
本文介绍了sprintf导致segmantation错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我正在使用RedHat Linux GCC 3.0。

我试图通过sprintf将long转换为字符串。

但是我得到了分段错误。

我也试过了snprintf但是没有用。这段代码就是..


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

long myLong = 200000;

char myStr [50 ];

memset [myStr,''\''',50];

sprintf(myStr,"%s",myLong);


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


这个sprintf导致分段错误。


希望得到帮助。


Saurabh

Hi all,

I am working on RedHat Linux GCC 3.0.
I am trying to convert a long to string through sprintf.
but i m getting segmantation fault.
I tried snprintf also but no avail.here is the piece of code..

----------------------------
long myLong=200000;
char myStr[50];
memset[myStr,''\0'',50];
sprintf(myStr,"%s",myLong);

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

This sprintf causes segmentation fault.

Hoping for help.

Saurabh

推荐答案

Saurabh写道:
Saurabh wrote:
大家好,

我正在研究RedHat Linux GCC 3.0。 />我试图通过sprintf将一个long转换为字符串。
但是我得到了segmantation错误。
我也尝试了snprintf但是没有用。还有一段代码..

--------- -------------------
long myLong = 200000;
char myStr [50];
memset [myStr,''\ 0'',50];
sprintf(myStr,"%s",myLong);
Hi all,

I am working on RedHat Linux GCC 3.0.
I am trying to convert a long to string through sprintf.
but i m getting segmantation fault.
I tried snprintf also but no avail.here is the piece of code..

----------------------------
long myLong=200000;
char myStr[50];
memset[myStr,''\0'',50];
sprintf(myStr,"%s",myLong);



^

%s用于打印(C风格)字符串。


使用%ld,或使用iostreams。


-

Ian Collins 。


^
%s is for printing (C style) strings.

use %ld, or use iostreams.

--
Ian Collins.




Ian Collins写道:

Ian Collins wrote:
Saurabh写道:
Saurabh wrote:
大家好,

我正在研究RedHat Linux GCC 3.0。
我正在尝试通过sprintf将一个long转换为字符串。
但是我得到了segmantation错误。
我试过了snprintf但也没有用。这段代码......

----------------------------
long myLong = 200000;
char myStr [50];
memset [myStr,''\ 0'',50];
sprintf(myStr,"%s" ,myLong);
Hi all,

I am working on RedHat Linux GCC 3.0.
I am trying to convert a long to string through sprintf.
but i m getting segmantation fault.
I tried snprintf also but no avail.here is the piece of code..

----------------------------
long myLong=200000;
char myStr[50];
memset[myStr,''\0'',50];
sprintf(myStr,"%s",myLong);


^%/%%s用于打印(C风格)字符串。

使用%ld,或使用iostreams。 />
-
Ian Collins。


^
%s is for printing (C style) strings.

use %ld, or use iostreams.

--
Ian Collins.




嗨Ian,

非常感谢。

你解决了我的问题。

实际上我认为

sprintf()的第二个参数指的是你要转换的格式

to,相反,它指的是你希望转换的格式。

无论如何..

再次感谢。

saurabh



hi Ian,
Thanks a lot.
You solved my problem.
actually I thought that the second argument to
sprintf() refers to the format you wish to convert
to,instead it refers to the format you wish to
convert from.
anyways..
thanks again.
saurabh




Saurabh写道:

Saurabh wrote:
Ian Collins写道:
Ian Collins wrote:
Saurabh写道:
Saurabh wrote:
大家好,

我正在研究RedHat Linux GCC 3.0。
我正在尝试将long转换为字符串sprintf。
但是我得到了分段错误。
我也尝试了snprintf但是没有用。这段代码就是这样的。

---------- ------------------
long myLong = 200000;
char myStr [50];
memset [myStr,''\ 0 '',50];
sprintf(myStr,"%s",my很长);
Hi all,

I am working on RedHat Linux GCC 3.0.
I am trying to convert a long to string through sprintf.
but i m getting segmantation fault.
I tried snprintf also but no avail.here is the piece of code..

----------------------------
long myLong=200000;
char myStr[50];
memset[myStr,''\0'',50];
sprintf(myStr,"%s",myLong);


^%/%%s用于打印(C风格)字符串。

使用%ld,或使用iostreams。
-
Ian Collins。


^
%s is for printing (C style) strings.

use %ld, or use iostreams.

--
Ian Collins.



您好,非常感谢。
您解决了我的问题。
实际上我认为
sprintf()的第二个参数指的是你希望转换为的格式,而是指你希望转换的格式。
无论如何..
再次感谢。
saurabh



hi Ian,
Thanks a lot.
You solved my problem.
actually I thought that the second argument to
sprintf() refers to the format you wish to convert
to,instead it refers to the format you wish to
convert from.
anyways..
thanks again.
saurabh



我建议如果你仍然使用sprinf,那么你还没有确定问题
,它肯定会打破一天。为什么不使用

boost :: lexical_cast(或者你自己的类似代码)。


I would suggest that if you are STILL using sprinf, then you haven''t
fixed the problem, it will surely break one day. Why not use
boost::lexical_cast (or you''re own similar code) instead.


这篇关于sprintf导致segmantation错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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