String class, how to implement sprintf?

Discussion in 'C++' started by CJ, Oct 28, 2003.

  1. CJ

    CJ Guest

    Ok, I've got a string class that stores the data in a dynamically allocated
    "char* m_pData" variable. I'd like to write a member function similiar to
    "sprintf" for this class.

    When implementing this member function, I'd like to just call "sprintf"
    under the covers. The problem is, that I dont' know how large the resulting
    string will be and thus I don't know how much space I'll need to allocate
    for my m_pData pointer.

    Is there any easy what to do this?

    Thanks!
    -CJ
     
    CJ, Oct 28, 2003
    #1
    1. Advertising

  2. "CJ" <> wrote in message
    news:...
    >
    > Ok, I've got a string class that stores the data in a dynamically

    allocated
    > "char* m_pData" variable. I'd like to write a member function similiar to
    > "sprintf" for this class.
    >
    > When implementing this member function, I'd like to just call "sprintf"
    > under the covers. The problem is, that I dont' know how large the

    resulting
    > string will be and thus I don't know how much space I'll need to allocate
    > for my m_pData pointer.
    >
    > Is there any easy what to do this?
    >
    > Thanks!
    > -CJ


    Many environments include a (non-standard) function called "snprintf /
    vsnprintf". It allows you to provide the maximum length of your buffer, and
    returns a special value if the buffer is too small. You can call that
    function repeteadly allocating larger and larger amounts of memory until the
    whole message fits in the buffer. It'll be a little slower, but it'll work.

    I guess you could also open a dummy file [like "/dev/null" (UNIX), or "NUL"
    (DOS, Windows), or just a normal temporary file], fprintf into it, grab the
    return value, allocate that amount of memory, and then call the normal
    sprintf. This might be even slower.

    Note: printf() & friends return the number of characters they emitted.

    D.
     
    Davlet Panech, Oct 28, 2003
    #2
    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. sprintf syntax for string to hex

    , Mar 24, 2005, in forum: C Programming
    Replies:
    3
    Views:
    799
    CBFalconer
    Mar 24, 2005
  2. jt
    Replies:
    3
    Views:
    966
    Keith Thompson
    May 23, 2005
  3. Replies:
    7
    Views:
    1,018
    Maxim Yegorushkin
    Jun 15, 2006
  4. Neal Barney

    Partial string loss with sprintf/strcat

    Neal Barney, Jul 22, 2006, in forum: C Programming
    Replies:
    9
    Views:
    401
    Neal Barney
    Aug 3, 2006
  5. Frank Neuhaus

    std::string sprintf

    Frank Neuhaus, Oct 7, 2005, in forum: C++
    Replies:
    11
    Views:
    7,024
    Jacques Labuschagne
    Oct 8, 2005
Loading...

Share This Page