Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > difficulties with sprintf function

Reply
Thread Tools

difficulties with sprintf function

 
 
Pilatus
Guest
Posts: n/a
 
      12-18-2003
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


 
Reply With Quote
 
 
 
 
Peter van Merkerk
Guest
Posts: n/a
 
      12-18-2003
"Pilatus" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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





 
Reply With Quote
 
 
 
 
Jeff Schwab
Guest
Posts: n/a
 
      12-18-2003
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
>
>


 
Reply With Quote
 
Pilatus
Guest
Posts: n/a
 
      12-18-2003

"Peter van Merkerk" <(E-Mail Removed)> a écrit dans le message de news:
brsd5f$74eih$(E-Mail Removed)-berlin.de...
> "Pilatus" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a function similar to C++'s sprintf Pep Java 5 08-25-2005 08:23 PM
function sprintf Michele C Programming 26 04-06-2004 06:46 PM
sprintf function Earth C Programming 19 02-09-2004 09:37 AM
sprintf function doesn't surrender to me!!! Yodai C Programming 2 01-14-2004 08:05 PM
atoi: stringstream or old C sprintf function Mike Chirico C++ 2 11-19-2003 03:59 PM



Advertisments