Search code examples
c++stringstring-formattingiostreamstdio

C++ standard replacement for (s)printf


I'm doing a server application in C++, and it provides an HTML page as response to HTTP requests.

The problem is that, currently, my webpage is written as a constant string in my code, and I insert other strings using << operator and std::stringstream, still during the writing of the string itself. See the example to get it clearer:

std::string first("foo");
std::string second("bar");
std::string third("foobar");

std::stringstream ss;
ss << "<html>\n"
    "<head>\n"
    "<title>Bitches Brew</title>\n"
    "</head>\n"
    "<body>\n"
    "First string: "
    << first << "\n"
    "Second string: "
    << second << "\n"
    "Third string: "
    << third << "\n"
    "</body>\n"
    "</html>";

Happens though I cannot simply stuff the contents in a file, because the data mixed with the HTML structure will change during the execution. This means I can't simply write the entire page in a file, with the string values of first, second, and third, because these values will change dynamically.

For the first request I'd send the page with first = "foo";, whereas in the second request I'd have first = "anything else".

Also, I could simply go back to sscanf/sprintf from stdio.h and insert the text I want -- I'd just have to replace the string gaps with the proper format (%s), read the HTML structure from a file, and insert whatever I wanted.

I'd like to do this in C++, without C library functions, but I couldn't figure out what to use to do this. What would be the C++ standard solution for this?


Solution

  • Standard C++ doesn't have a direct equivalent to (s)printf-like formatting other than (s)printf itself. However, there are plenty of formatting libraries that provide this functionality, like the cppformat library that includes a C++ implementation of Python's str.format and safe printf.

    That said, I'd recommend using a template engine instead, see C++ HTML template framework, templatizing library, HTML generator library .

    Or you can always reinvent the wheel and write your own template engine by reading a file and replacing some placeholders with arguments.