Results 1 to 7 of 7

Thread: c++ equivalent function to the c-function 'sprintf

  1. #1
    Join Date
    Aug 2006
    Posts
    253

    c++ equivalent function to the c-function 'sprintf

    Hi

    Is there a c++ equivalent function to the c-function 'sprintf'?

    Thnaks

  2. #2
    Join Date
    Oct 2008
    Posts
    43

    Re: c++ equivalent function to the c-function 'sprintf

    You can also check out boost::format, which can be easier to work with than stringstream

  3. #3
    Join Date
    Aug 2006
    Posts
    253

    Re: c++ equivalent function to the c-function 'sprintf

    thank you, that looks much more convenient!

  4. #4
    Join Date
    Mar 2008
    Posts
    52

    Re: c++ equivalent function to the c-function 'sprintf

    I guess it's rather ::sprintf (with extern "C" { / #include <stdio.h> / }).

    But if you want some safety, you'd use ::snprintf, and if you don't want to implement the safety yourself, you could rather use lisp^W std:stringstream.

    std:stringstream s; s<<"Hi "<<world<<std::endl;
    std::string str =s.str();
    const char* cstr=s.c_str();

  5. #5
    Join Date
    Mar 2008
    Posts
    252

    Re: c++ equivalent function to the c-function 'sprintf

    C functions are in the "root" namespace; to avoid
    refering a different object in the current namespace, it's advised (in various style guides) to qualify C functions.

    Then, using a function without declaring it would make a sane compiler complain, so it's good practice to include some header declaring it before using it.

  6. #6
    Join Date
    Oct 2008
    Posts
    20

    Re: c++ equivalent function to the c-function 'sprintf

    The is the C++ group here, so the C header <stdio.h> is to be
    included using <cstdio> and its content is accessible only within
    the 'std' namespace.

  7. #7
    Join Date
    Dec 2011
    Posts
    1

    Re: c++ equivalent function to the c-function 'sprintf

    This is works well, works like a sprintf for c++

    Code:
    std::string string_format(const std::string &fmt, ...) {
           int n, size=100;
           std::string str;
           va_list ap;
           while (1) {
           str.resize(size);
           va_start(ap, fmt);
           int n = vsnprintf((char *)str.c_str(), size, fmt.c_str(), ap);
           va_end(ap);
           if (n > -1 && n < size)
               return str;
           if (n > -1)
               size=n+1;
           else
               size*=2;
           }
    }

Similar Threads

  1. C# Equivalent Of Javascript's Location.replace() Function?
    By ASHER in forum Software Development
    Replies: 6
    Last Post: 16-05-2010, 01:20 AM
  2. c# function equivalent to gettime function in javascript
    By Omaar in forum Software Development
    Replies: 4
    Last Post: 10-03-2010, 10:44 PM
  3. How does abstract function differs from virtual function?
    By Maddox G in forum Software Development
    Replies: 5
    Last Post: 29-01-2010, 11:32 AM
  4. How to implement the INITCAP equivalent function in SQL Server
    By TechPredator in forum Software Development
    Replies: 3
    Last Post: 06-08-2009, 03:33 PM
  5. Function keys don't function under windows XP
    By GunFighter in forum Hardware Peripherals
    Replies: 3
    Last Post: 08-04-2009, 11:07 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,045,937.15044 seconds with 17 queries