2

I'm used to programming in C but now I needed to use C++ for a project, and I need to save some text into a string, the code I would have used in C would have been

sprintf(pathFotosLimpias, "CleanPictures/Picture_T%d_%d", pictureNumber, templateNumber);

Or something like that, but pathFotosLimpias is a string so it doesn't work and I can't find how to save it as a sting, I think there is a boost function which does something similar to what I need but I can't figure out what exactly should I do, can someone explain what I need and maybe give me an example on how to use it?

Thank you.

Update: It is apparently a cv::String from opencv, I'm a bit more confused now. Nevermind, that was an error.

SantiMar
  • 59
  • 1
  • 6
  • You cannot really use `sprintf` with `std::strings`. Use `std::ostringstream` instead. –  Jul 28 '18 at 22:38
  • In response to your edit, I don't know anything about the `cv::String` type, but it is probably incompatible with use of `sprintf`. If you are wedded to using that function, then you need to output into a C-style string (i.e. a char array) and then convert that to the type you need. But tjis is not exactly optimal. –  Jul 28 '18 at 22:49
  • Maybe you can use something like `std::stringstream`. You can use then the operator `<<` to load arbitray content in the stream. Then call the method `str()` to retrieve the string. – Alberto12 Jul 28 '18 at 23:29
  • I looked it up and yes, that seems to be what i want, but i'm having a hard time understanding how it works. Should `pathFotosLimpias << "CleanPictures/Picture_T" << pictureNumber << templateNumber ` work? Do you have any link to somewhere where I could learn how to use it? – SantiMar Jul 29 '18 at 01:01
  • https://stackoverflow.com/questions/2342162/stdstring-formatting-like-sprintf – xskxzr Jul 30 '18 at 04:17

2 Answers2

2

You can either directly add strings:

#include <string> //put this at the top
auto path = std::string("CleanPictures/Picture_T") + std::to_string(pictureNumber) + "_" + std::to_string(templateNumber);

(Here the "_" is implicitly converted to std::string)

Or use stringstreams:

#include <sstream> //put this at the top
auto stream = std::stringstream{};
stream << "CleanPictures/Picture_T" << pictureNumber << '_' << templateNumber;
auto path = stream.str(); //get the string from the stream

Another possibility would be to use abseil, which provides a type safe replacement for printf:

auto path = absl::StrFormat("CleanPictures/Picture_T%d_%d", pictureNumber, templateNumber);

From all these possibilities I would recommend:

  1. Use stringstreams if you are concerned about type safety.
  2. Use std::to_string if you want some more performant code.
  3. Use abseil if you are concerned about type safety and performance.

EDIT:

Another possibility for generating strings is the fmt library. I would put it at the top of my recommendations now, as it provides a wide range of formatting options (using the python formatting style) and is easy to read/use.

#include <fmt/format.h>
std::string path = fmt::format("CleanPictures/Picture_T{:d}_{:d}", pictureNumber, templateNumber);
jan.sende
  • 750
  • 6
  • 23
1

You can use std::sprintf() in C++, similar to how you use sprintf() in C:

#include <cstdio>

char pathFotosLimpias[...];
std::sprintf(pathFotosLimpias, "CleanPictures/Picture_T%d_%d", pictureNumber, templateNumber);

However, you should use std::ostringstream in C++ instead:

#include <string>
#include <sstream>

std::ostringstream oss;
oss << "CleanPictures/Picture_T" << pictureNumber << "_" << templateNumber;
std::string pathFotosLimpias = oss.str();
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770