1
const char* email = "[email protected]";
const char* password = "123456";
          data.add(R"({
             "name": "{*MY_EMAIL_HERE*}",
             "password": "{*MY_PASSWORD_HERE*}",
             "date": { "day": 20, "month": "Apr" }
          })");

I'm looking for fast way to do it like u do in C language with printf("%s", myString);

7
  • 3
    I was about to reply back and say "stringstream". But, then I noticed you are trying to build a json string. Any solution using a generic formatting technique is going to blow up the moment the password contains a ", {, }, or similar character that requires json escaping. If someone on my team put out a pull request where they were manually formatting a JSON or XML blob, I would immediately tell them to go use one of the standard parsing/formatting libraries we already have in our code base that guarantees escaping. Google for "json library" or similar. Millions of libs avaialble. Commented Apr 14, 2020 at 15:34
  • @selbie thanks for the answer Commented Apr 14, 2020 at 21:06
  • 1
    Is this for a homework assignment? You don't want to store passwords. Ever. You can store hashes of them, but that requires lots of care as well and there are lots of examples that do it wrong. Commented Apr 14, 2020 at 22:01
  • 1
    @RANOK I suggest you use some existing solution for authentication - there's quite a few to choose from. Don't hash the password yourself. You'll be unlikely to get it right. There's really a lot of finesse to implementing a secure authentication solution, and textbook-style approaches will be universally insecure and/or convoluted. Authentication is a solved problem, and you shouldn't be reimplementing the wheel. Remember that you can't only look at the hashing code, but the whole thing: from the client onwards. The weakest link will determine the overall safety of it. Commented Apr 15, 2020 at 21:54
  • 1
    @RANOK MD5 hasn't been acceptable for passwords for at least a decade. Please do some research at the beginning phase of your project. Commented Apr 15, 2020 at 21:59

1 Answer 1

3

Solution_1:

Modern C++ makes this super simple.

C++20

General case

C++20 introduces std::format, which allows you to do exactly that. It uses replacement fields similar to those in python:

#include <iostream>
#include <format>

int main() {
    std::cout << std::format("Hello {}!\n", "world");
}

Check out the full documentation! It's a huge quality-of-life improvement.

Solution_2:

in this case

using nlohmann/json (https://github.com/nlohmann/json)

 #include <nlohmann/json.hpp>

 // for convenience
 using json = nlohmann::json;
 // create an empty structure (null)
 json j;

 // add a number that is stored as double (note the implicit conversion of j to an object)
 j["pi"] = 3.141;

 // add a Boolean that is stored as bool
 j["happy"] = true;

 // add a string that is stored as std::string
 j["name"] = "Niels";

 // add another null object by passing nullptr
 j["nothing"] = nullptr;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.