sprintf in C++

This is a discussion on sprintf in C++ within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm in a habit of using sprintf in C++. What in the standard C++ can be use instead? and ...

  1. #1
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116

    sprintf in C++

    Hi,
    I'm in a habit of using sprintf in C++.
    What in the standard C++ can be use instead? and should I change my ways?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    stringstream is the "similar thing". You use it like any other stream, and it's a "two way", so you can do something like:
    Code:
    int main() 
    {
      stringstream ss;
      float f;
      string s;
      cout << "Enter a float:";
      cin >> f;
      ss << setw(5) << setprecision(3) << f;
      ss >> s;
      cout << "string is " << s << endl;
      return 0;
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116
    Thanks matsp.

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, 05:33 PM
  2. sprintf : garbage appended
    By yeller in forum C Programming
    Replies: 9
    Last Post: 12-17-2007, 10:21 AM
  3. sprintf in C and C++
    By usu_vlsi in forum C++ Programming
    Replies: 2
    Last Post: 03-14-2005, 04:14 AM
  4. sprintf and sscanf
    By tommy69 in forum C Programming
    Replies: 10
    Last Post: 04-22-2004, 09:00 PM
  5. Sprintf
    By Trauts in forum C++ Programming
    Replies: 10
    Last Post: 01-15-2003, 01:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21