difficulties with sprintf function

Discussion in 'C++' started by Pilatus, Dec 18, 2003.

  1. Pilatus

    Pilatus Guest

    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
    Pilatus, Dec 18, 2003
    #1
    1. Advertising

  2. "Pilatus" <> wrote in message
    news:...
    > 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!
    --
    Peter van Merkerk
    peter.van.merkerk(at)dse.nl
    Peter van Merkerk, Dec 18, 2003
    #2
    1. Advertising

  3. Pilatus

    Jeff Schwab Guest

    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 ;
    > 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
    >
    >
    Jeff Schwab, Dec 18, 2003
    #3
  4. Pilatus

    Pilatus Guest

    "Peter van Merkerk" <> a écrit dans le message de news:
    brsd5f$74eih$-berlin.de...
    > "Pilatus" <> wrote in message
    > news:...
    > > 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!
    > --
    > Peter van Merkerk
    > peter.van.merkerk(at)dse.nl


    thank's for your help!! merci beaucoup!

    Pilatus
    Pilatus, Dec 18, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pep
    Replies:
    5
    Views:
    4,046
  2. Mike Chirico
    Replies:
    2
    Views:
    3,818
    Grumble
    Nov 19, 2003
  3. Yodai
    Replies:
    2
    Views:
    437
    Alex Monjushko
    Jan 14, 2004
  4. Earth

    sprintf function

    Earth, Feb 8, 2004, in forum: C Programming
    Replies:
    19
    Views:
    6,422
    Irrwahn Grausewitz
    Feb 9, 2004
  5. Michele

    function sprintf

    Michele, Apr 1, 2004, in forum: C Programming
    Replies:
    26
    Views:
    1,020
    Christopher Benson-Manica
    Apr 6, 2004
Loading...

Share This Page