0

I am attempting to construct a serial number of a certain format. This number will be entered into a database. At this point I am having to use sprintf, but I would like a native C++ method for it.

Here is sample code:

int i;

sprintf(buffer, "%03d", i);

The integer will be anywhere from 1 to 3 digits. The format needs to look like this:

001, ... 013, ... 101, ... etc.

The "serial number" has the format:

AAAAA001, ... AAAAA013, ... AAAAA101, etc.

So the question is, is there a way to do this that is native to C++ without having to use iostream manipulators and that is included in the mingw-w64 libraries. Or does it require something like boost libraries?

Another way to put it: is there a drop-in replacement in C++ for the C sprintf function?

Edit based upon comments:

So there is nothing as simple as....

int i;
string buffer;

sprintf(buffer, "%03d", i);

I realize that this does not work, but it gives the thought anyway. There is no way to operate directly on a string class object with a method that serves the function of sprintf?

pwn01
  • 31
  • 1
  • 8
  • There's [`std::to_string`](http://en.cppreference.com/w/cpp/string/basic_string/to_string) but that doesn't allow for specifying format. Even `boost::format` uses string streams under the hood. – sjdowling Feb 16 '15 at 15:58
  • So it won't "taint the purity" of the c++ code to use sprintf in the end? – pwn01 Feb 16 '15 at 16:02
  • 3
    [`std::sprintf`](http://en.cppreference.com/w/cpp/io/c/fprintf) is part of the C++ standard as much as IO streams are and has the advantage of being significantly faster than any formatting library using streams. However since it originates from C it is especially difficult to use safely and you may want to look at `std::snprintf` instead. – sjdowling Feb 16 '15 at 16:58
  • When you say "safely" do you mean that it has platform dependent characteristics? – pwn01 Feb 16 '15 at 17:06
  • I mean that is is neither type safe nor memory safe, [using it without utmost care can result in bugs or outright security vulnerabilities](http://stackoverflow.com/questions/3662899/understanding-the-dangers-of-sprintf) – sjdowling Feb 16 '15 at 17:12
  • Okay. I looked at your link and can understand that. I've edited the original question to make it more precise. – pwn01 Feb 16 '15 at 17:18
  • I think the real question you have to answer first is "why do you not want to use `sstream` "? Without establishing that first, it's a bit of an XYProblem, since `sstream` will certainly solve the given problem clearly, type-safely and relatively efficiently. – aruisdante Feb 16 '15 at 17:38
  • Since you know ahead of time exactly how many characters your formatted string will have, `std::snprintf` with a fixed-size buffer ought to be a fast, safe and convenient solution. You can easily create a `std::string` from the `char[]` buffer later if you need to. – 5gon12eder Feb 16 '15 at 18:16
  • I hadn't come across sstream when I was looking around. Since it seems to have associated formatting functions, it may do the trick. I guess that my understanding of streams was too narrow. Thanks. aruisdante If you will enter your suggestion as the answer to the question. I will accept that as the answer. – pwn01 Feb 16 '15 at 18:18

1 Answers1

1

I'm taking aruisdante's answer as the best answer to the question although it is a stream which I initially found undesirable.

I think the real question you have to answer first is "why do you not want >to use sstream "? Without establishing that first, it's a bit of an >XYProblem, since sstream will certainly solve the given problem clearly, >type-safely and relatively efficiently. – aruisdante

My understanding of streams was too narrow. It looks like a stringstream should work well for my application.

Thanks again.

pwn01
  • 31
  • 1
  • 8