Thread: sprintf equivalence in C++

  1. #1
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198

    sprintf equivalence in C++

    Hi,

    I was getting some help at the C forum, on converting an int to a char[]. Quzah suggested the use of sprintf, but now I have decided to implement what I am doing in C++, rather than C, and I am getting compilation errors with sprintf.

    Is there an equivalent function of sprint in C++? or is there another way to approach the conversion.


    I wasnt sure if I should get the post moved, a mod can merge them if they feel they should be.

    Thanks.
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by MethodMan
    Hi,

    I was getting some help at the C forum, on converting an int to a char[]. Quzah suggested the use of sprintf, but now I have decided to implement what I am doing in C++, rather than C, and I am getting compilation errors with sprintf.

    Is there an equivalent function of sprint in C++? or is there another way to approach the conversion.


    I wasnt sure if I should get the post moved, a mod can merge them if they feel they should be.

    Thanks.
    sprintf() is available in C++. Try this:
    Code:
    #include <iostream>
    
    using namespace std
    
    int main()
    {
      char *temp="Hello";
      char temp2[100];
      sprintf(temp2, "%s World", temp);
      cout << "temp: <" << temp << ">" << endl;
      cout << "temp2: <" << temp2 << ">" << endl;
    
      return 0;
    }
    If it doesn't work, post your compiler errors


    Regards,

    Dave

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    If you decide not to use sprintf, the "other way to approach the conversion" is to use stringstreams:

    Code:
    #include <iostream>
    #include <strstream>
    using namespace std;
    
    int main()
    {
        strstream temp;
        int data = 15;
        char buffer[20];
    
        temp << data;     // Store int in strstream
        temp >> buffer;   // Read int value back into char buffer
    
        cout << buffer << endl;
    
        return 0;
    }

    Or...

    Code:
    #include <iostream>
    #include <sstream>
    #include <string>
    using namespace std;
    
    int main()
    {
        stringstream temp;
        int data = 15;
        string buffer;
    
        temp << data;     // Store int in stringstream
        temp >> buffer;   // Read int value back into string
    
        cout << buffer << endl;
    
        return 0;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    Im getting a cast error, I must be calling sprintf() wrong

    //funtion prototype
    void Timer:rawText(GLint x, GLint y, char* s, GLfloat r, GLfloat g, GLfloat b);

    int i = 10; // want that to be used in my function, need to convert it
    char buf[256];

    sprintf(buf, sizeof(buf), i);

    And then I would use buf in my function call.

    Edit:

    strstream' : undeclared identifier

    I included the header file, but I get the following error....

    Im using Visual Studio
    Last edited by MethodMan; 11-21-2004 at 06:31 PM.
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by MethodMan
    Im getting a cast error, I must be calling sprintf() wrong


    int i = 10; // want that to be used in my function, need to convert it
    char buf[256];

    sprintf(buf, sizeof(buf), i);
    Well, you are correct: that is wrong. You need a format specifier, like in printf().

    If you already had sprintf() working in C, I think you could use in C++ exactly the same way as you did.

    Given your present status, I see two possibilities:

    1. Learn to use sprintf().

    2. Learn to use stringstreams.

    If you are going to use C++, maybe your time would be better spent learning about stringstreams, as was suggested by hk_mp5kpdw.

    Regards,

    Dave

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Yeah, you don't need the size, and you do need a format specifier. Here is a link: http://cppreference.com/stdio_details.html#sprintf

    I really would recommend stringstreams with strings, though (hk_mp5kpdw's second example). That is a much less error prone solution.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Wouldn't it be better to use the ostringstream class? I think strstream is deprecated
    Code:
    #include <iostream>
    #include <sstream>
    using namespace std;
    
    int main()
    {
        ostringstream temp;
        int data = 15;
        char buffer[20];
    
        temp << data << flush;     // Store int into stream
        temp.str().copy(buffer, 20);   // Read int value back into char buffer
        cout << buffer << endl;
        cin.get();
        return 0;
    }
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Using your example:
    Code:
    int i = 10; // want that to be used in my function, need to convert it
    char buf[256];
    
    sprintf(buf, "%d", i);
    It works the same as printf(), just outputs to a string versus the screen.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sprintf overflows my buffer -- why?
    By Lasston in forum C Programming
    Replies: 26
    Last Post: 06-20-2008, 04:33 PM
  2. sprintf : garbage appended
    By yeller in forum C Programming
    Replies: 9
    Last Post: 12-17-2007, 10:21 AM
  3. sprintf() giving crash to program!!
    By maven in forum C Programming
    Replies: 4
    Last Post: 01-01-2006, 12:26 PM
  4. sprintf and sscanf
    By tommy69 in forum C Programming
    Replies: 10
    Last Post: 04-22-2004, 08:00 PM
  5. Sprintf
    By Trauts in forum C++ Programming
    Replies: 10
    Last Post: 01-15-2003, 01:35 PM