473,498 Members | 1,656 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

difficulties with sprintf function

Hi everybody,

I am beginner and I have a problem in this simple code : I want that the
char contents 01234567, but it don't work
Could you help me ?
int bin[8] ;
char *buf = new char [4];
for (int i = 0; i <= 7; i++)
{
tab[i] = i ;
cout << tab [i];}

sprintf(buf, "%d", tab);
cout << "\nbuf=" << buf;
delete [ ] buf;

Thank's a lot for your advices and sorry for my bad english

Pilatus
Jul 22 '05 #1
3 1879
"Pilatus" <ra*******@freesurf.fr> wrote in message
news:10****************@lotis.uk.clara.net...
Hi everybody,

I am beginner and I have a problem in this simple code : I want that the
char contents 01234567, but it don't work
Could you help me ?
int bin[8] ;
char *buf = new char [4];
Here you don't allocate enough memory to store '01234567' plus the zero
terminator; you will need to allocate at least a char array with 9
elements. If your program writes beyond the allocated array (which it does
when the sprintf() function is called) anything may happen; the program may
crash immediately, it may crash later at some arbitrary point or it may
even appear to work fine (but if murphy gets its way not at the customers
site).
for (int i = 0; i <= 7; i++)
{
tab[i] = i ;
cout << tab [i];}

sprintf(buf, "%d", tab);
You cannot convert an array to a char array like this. The "%d" format
specifier means that the sprintf function expects one integer to follow.
However you give it a pointer to the first element of the tab array. To
convert the numbers in an integer array you will have to do that one number
at a time; i.e. you will need a loop for this.
cout << "\nbuf=" << buf;
delete [ ] buf;


Rather than using C tricks, try to solve the problem the C++ way. Using the
std::string class and the std::stringstream you don't have to worry about
buffer overflows or format specifiers:

#include <iostream>
#include <sstream>
using namespace std;

int main()
{
int tab[8];

// Fill tab array...
for(int i = 0; i <= 7; ++i)
{
tab[i] = i;
cout << tab [i];
}

cout << endl;

// Convert to string.
stringstream ss;
for(int j = 0; j <= 7; ++j)
{
ss << tab[j];
}

string s = ss.str();

// Output string.
cout << s << endl;

return 0;
}

Good luck!
--
Peter van Merkerk
peter.van.merkerk(at)dse.nl

Jul 22 '05 #2
Pilatus wrote:
Hi everybody,

I am beginner and I have a problem in this simple code : I want that the
char contents 01234567, but it don't work
Could you help me ?
char s[ ] = "01234567";
int bin[8] ;
char *buf = new char [4];
for (int i = 0; i <= 7; i++)
{
tab[i] = i ;
cout << tab [i];}

sprintf(buf, "%d", tab);
cout << "\nbuf=" << buf;
delete [ ] buf;

Thank's a lot for your advices and sorry for my bad english

Pilatus


Jul 22 '05 #3

"Peter van Merkerk" <me*****@deadspam.com> a écrit dans le message de news:
br************@ID-133164.news.uni-berlin.de...
"Pilatus" <ra*******@freesurf.fr> wrote in message
news:10****************@lotis.uk.clara.net...
Hi everybody,

I am beginner and I have a problem in this simple code : I want that the
char contents 01234567, but it don't work
Could you help me ?
int bin[8] ;
char *buf = new char [4];
Here you don't allocate enough memory to store '01234567' plus the zero
terminator; you will need to allocate at least a char array with 9
elements. If your program writes beyond the allocated array (which it does
when the sprintf() function is called) anything may happen; the program

may crash immediately, it may crash later at some arbitrary point or it may
even appear to work fine (but if murphy gets its way not at the customers
site).
for (int i = 0; i <= 7; i++)
{
tab[i] = i ;
cout << tab [i];}

sprintf(buf, "%d", tab);
You cannot convert an array to a char array like this. The "%d" format
specifier means that the sprintf function expects one integer to follow.
However you give it a pointer to the first element of the tab array. To
convert the numbers in an integer array you will have to do that one

number at a time; i.e. you will need a loop for this.
cout << "\nbuf=" << buf;
delete [ ] buf;
Rather than using C tricks, try to solve the problem the C++ way. Using

the std::string class and the std::stringstream you don't have to worry about
buffer overflows or format specifiers:

#include <iostream>
#include <sstream>
using namespace std;

int main()
{
int tab[8];

// Fill tab array...
for(int i = 0; i <= 7; ++i)
{
tab[i] = i;
cout << tab [i];
}

cout << endl;

// Convert to string.
stringstream ss;
for(int j = 0; j <= 7; ++j)
{
ss << tab[j];
}

string s = ss.str();

// Output string.
cout << s << endl;

return 0;
}

Good luck!
--
Peter van Merkerk
peter.van.merkerk(at)dse.nl


thank's for your help!! merci beaucoup!

Pilatus

Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
26730
by: Earth | last post by:
Hi all, I am new to c and trying the sprintf function. I have written a testing program to try the sprintf fuction and expect the output is 1.234. However, the output shows nothing. Am I...
32
440
by: Michele | last post by:
I have a code line like this: sprintf(buf, "%20g",*(liftPtr++)) what does it mean? And what does the function sprintf do? Thanks michele
3
2212
by: portergrouptx | last post by:
I am trying to pad a string with leading character zeros. There seems to be a difference between the behavior of sprintf on Windows (Microsoft Visual C++ .NET) and on MVS. Can anyone explain the...
1
3503
by: jimjim | last post by:
Hello, I was wondering about the implications of giving as an argument to sprintf a different data type from the one specified in the format argument. This type of question along with some...
4
2362
by: ypjofficial | last post by:
Hello All, To use sprintf function we have to first create a char * and assign some memory to it or we have to fixed memory sized array. eg. char str;//or it can be //char * str =(char...
12
15185
by: babak | last post by:
Hi everyone I want to format a string (using sprintf) and put it in a messagebox but however I try to do it, it doesn't seem to work. Here is an example sample of what i try to do: char msg;...
6
2643
by: merrittr | last post by:
I am trying to build variables for a function using sprintf. However they don't seem to be proper char strings since submiting literals seems to work fine. Any advice to get me rolling? ...
15
3500
by: krister | last post by:
Hello, I'm working in a quite large system that has some limitations. One of those is that I can't use printf() to get an output on a screen. I'm forced to use a special function, let's call it...
173
13758
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...
0
7002
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7205
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7379
tracyyun
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...
0
5462
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4910
isladogs
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...
0
3093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1419
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
291
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.