Print std::string

Hey guys, I am trying to call a method which was declared like so:

 
  const std::string& getFirmwareVersion() const { return m_ecuFirmwareVersion; }


so that I can print the m_ecuFirmwareVersion which was declared like so:

 
  std::string m_ecuHardwareVersion;


here is my print method:

 
cout << printf("\nThe version is: %s", NovariantModule::getHardwareVersion().c_str());


it is printing 17 or 18 or sometimes 19 this value shouldn't be changing I know that so I am wondering if perhaps I am trying to print it incorrectly...
You're calling printf, and then printing the return value of printf to cout (printf returns the number of characters written).
You probably just want: cout << "\nThe version is: " << NovariantModule::getHardwareVersion() << '\n';

Still. if the number is changing, then that still most likely means that getHardwareVersion() is returning a different string than before. Are you absolutely sure it's not?

The following prints out the same thing for me each time:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Example program
#include <iostream>
#include <cstdio>
#include <string>

namespace NovariantModule {
   std::string getHardwareVersion()
   {
        return "1.2.3.4";   
   }
}

int main()
{
    using namespace std;
    
    cout << printf("\nThe version is: %s", NovariantModule::getHardwareVersion().c_str());
    cout << printf("\nThe version is: %s", NovariantModule::getHardwareVersion().c_str());
    cout << printf("\nThe version is: %s", NovariantModule::getHardwareVersion().c_str());
}


The version is: 1.2.3.424
The version is: 1.2.3.424
The version is: 1.2.3.424 
Last edited on
Okay everything is working using cout. Thanks.
Until recently if a programmer wanted printf-like functionality in a type-safe C++ manner they used a 3rd party library. Boost has the Format library:
https://www.boost.org/doc/libs/1_76_0/libs/format/doc/format.html

(Are there other libraries that provide similar functionality? I don't know)

C++20 changed that, there is now the <format> library. Part of the C++ standard:
https://en.cppreference.com/w/cpp/utility/format/format
Are there other libraries that provide similar functionality?

There are a few alternatives, most notably "fmt", which is dramatically superior to Boost.Format in terms of performance, code size, and compilation speed.
https://fmt.dev/latest/index.html
Last edited on
Is fmt still "better" (I know, opinion) than using C++20's <format>?

I'd prefer to use the C++ standard version over a 3rd party library if I need printf-like formatting. Usually I don't need it. "Straight" stream output works for me.
As of C++20 <format> requires the programmer to pointlessly allocate a std::string just to print some characters on the screen, as in
std::cout << std::format("{} {}\n", "hello", "world");
but if this is undesirable the programmer can use a ridiculous iterator-based interface like
std::format_to(std::ostreambuf_iterator<char>{std::cout}, "{} {}\n", "hello", "world");

Contrast this with fmt which actually has a function that lets you print stuff to a stream.
Also fmt but not std::format can understand format strings at compile-time, and is therefore faster, in at least in theory.
Last edited on
Topic archived. No new replies allowed.