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 10 4163
Saurabh wrote: 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 is for printing (C style) strings.
use %ld, or use iostreams.
--
Ian Collins.
Ian Collins wrote: Saurabh wrote: 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 is for printing (C style) strings.
use %ld, or use iostreams.
-- Ian Collins.
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 wrote: Ian Collins wrote: Saurabh wrote: 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 is for printing (C style) strings.
use %ld, or use iostreams.
-- Ian Collins.
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
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.
Geo wrote: Saurabh wrote: Ian Collins wrote: Saurabh wrote: > 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 is for printing (C style) strings.
use %ld, or use iostreams.
-- Ian Collins.
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
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.
hi Geo,
I have never used boost::lexical_cast.Is this an STL component?
or are you hinting me to write my own version of sprintf()?
Thanks & Regards
saurabh
Saurabh wrote: Geo wrote: Saurabh wrote: Ian Collins wrote: > Saurabh wrote: > > 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 is for printing (C style) strings. > > use %ld, or use iostreams. > > -- > Ian Collins.
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
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.
hi Geo, I have never used boost::lexical_cast.Is this an STL component? or are you hinting me to write my own version of sprintf()?
Thanks & Regards saurabh
Hi,
No I was suggesting you write you're own lexical cast function, if you
can't use boost. You could start with something like this, you may want
to make this more robust,
template<typename out, typename in> out lexical_cast(const in &i)
{
std::stringstream ss;
ss << i;
out temp;
ss >> temp;
return temp;
}
then do
int x = 123;
std::string s = lexical_cast<std::string>(x);
you can also do the reverse
std::string s = "5678";
int z = lexical_cast<int>(s);
Saurabh wrote: memset[myStr,'\0',50];
Take this line out of your code.
Old Wolf wrote: Saurabh wrote:
memset[myStr,'\0',50];
Take this line out of your code.
Yep, Agree.
And use %d instead of %s.It makes your program think that your 'MyLong'
as a pointer to characters.
Saurabh wrote: I have never used boost::lexical_cast.Is this an STL component? or are you hinting me to write my own version of sprintf()?
Thanks & Regards saurabh
I would not suggest boost::lexical_cast here. The boost equivalent of
sprintf is boost::format.
lexical_cast is useful for converting the other way, i.e. string to
integer.
Geo wrote: Saurabh wrote: Geo wrote: Saurabh wrote: > Ian Collins wrote: > > Saurabh wrote: > > > 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 is for printing (C style) strings. > > > > use %ld, or use iostreams. > > > > -- > > Ian Collins. > > 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
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. hi Geo, I have never used boost::lexical_cast.Is this an STL component? or are you hinting me to write my own version of sprintf()?
Thanks & Regards saurabh
Hi,
No I was suggesting you write you're own lexical cast function, if you can't use boost. You could start with something like this, you may want to make this more robust,
template<typename out, typename in> out lexical_cast(const in &i) { std::stringstream ss; ss << i; out temp; ss >> temp; return temp; }
but g++ told me that:
In function `out lexical_cast(const in&) [with out = std::string, in =
int]':
instantiated from here
error: `ss' has incomplete type
error: storage size of `ss' isn't known then do
int x = 123; std::string s = lexical_cast<std::string>(x);
you can also do the reverse
std::string s = "5678"; int z = lexical_cast<int>(s);
Noclambulist <No**********@gmail.com> wrote: Geo wrote: No I was suggesting you write you're own lexical cast function, if you can't use boost. You could start with something like this, you may want to make this more robust,
template<typename out, typename in> out lexical_cast(const in &i) { std::stringstream ss; ss << i; out temp; ss >> temp; return temp; } but g++ told me that: In function `out lexical_cast(const in&) [with out = std::string, in = int]': instantiated from here error: `ss' has incomplete type error: storage size of `ss' isn't known
You need #include <sstream> for std::stringstream.
--
Marcus Kwok
Replace 'invalid' with 'net' to reply This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Dave |
last post by:
I have a multithrading program(daemon server) written by c++(GCC)
under AIX4.X, I get segmentation fault problem when it runs several
hours with heavy load. I want to know which compilation option...
|
by: sandwich_eater |
last post by:
I get a segmentation fault in my program when calling a function
"TestFn" that has been passed as a pointer into another function.
The following excerpt should give enough information as to what I...
|
by: steve |
last post by:
Well I've been working all morning and have finally found the source of
my "bus error (signal 10)" errors. The source is odd. The error
occurs in any function where I make the function call:
...
|
by: Neal Barney |
last post by:
I have a C program which runs on a device using a Zilog Z180
microprocessor. While it can address 1MB of RAM, it can only address
64KB at any given time. And of that only 16KB can be used for...
|
by: dschulenburg |
last post by:
I have a function that runs, but gets a segmentation fault if I make very minor changes. For example adding a print statment somwhere into the function works, adding the same print statment twice at...
|
by: Jess |
last post by:
Hello, the following code failed with "segmentation fault". Could
someone tell me why it failed please? Thanks!
#include<iostream>
#include<string>
#include<cstring>
#include<cstddef>
using...
|
by: sam_cit |
last post by:
Hi Everyone,
I have the following program unit,
#include <stdlib.h>
int main()
{
char *p = (char*)malloc(100);
if(p==NULL)
|
by: Olaf |
last post by:
Hi,
does exist a safe version/way of sprintf to prevent a buffer overflow by
using in this manner?
char* format = "0x%0.4X\n";
char buf;
sprintf(buf, format, number);
|
by: Ron Ford |
last post by:
I'm looking for a freeware c99 compiler for windows. I had intended to use
MS's Visual C++ Express and use its C capability. In the past with my MS
products, I've simply needed to make .c the...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |