difficulties with sprintf function

P

Pilatus

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 ;
cout << tab ;}

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

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

Pilatus
 
P

Peter van Merkerk

Pilatus said:
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 ;
cout << tab ;}

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;
cout << tab ;
}

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!
 
J

Jeff Schwab

Pilatus said:
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 ;
cout << tab ;}

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

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

Pilatus
 
P

Pilatus

Peter van Merkerk said:
Pilatus said:
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 ;
cout << tab ;}

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;
cout << tab ;
}

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!


thank's for your help!! merci beaucoup!

Pilatus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top