convert sprintf call to ???

L

Lynn McGuire

Is there a good way to convert this sprintf call to C++ code with
type safety ? The %4.4s is critical to the formatting.

std::string name;
std::string number;
char string3 [100];
sprintf_s (string3, sizeof (string3), "*** PROCESS RATING - HEAT EXCHANGER %4.4s = %s",
number.c_str (), name.c_str ());

Thanks,
Lynn
 
L

LR

Lynn said:
Is there a good way to convert this sprintf call to C++ code with
type safety ? The %4.4s is critical to the formatting.

std::string name;
std::string number;
char string3 [100];
sprintf_s (string3, sizeof (string3), "*** PROCESS RATING - HEAT EXCHANGER %4.4s = %s",
number.c_str (), name.c_str ());


Try,

std::eek:stringstream o3;
o3 << "*** PROCESS RATING - HEAT EXCHANGER ";
o3 << std::setw(4) << number.substr(0,4); // beware magic numbers
o3 << " = ";
o3 << name;

You might end up with something more than sizeof(string3) chars, if
that's important as well you might want to,

o3.str().substr(0,100); // beware magic numbers.

LR
 
L

Lynn McGuire

std::eek:stringstream o3;
o3<< "*** PROCESS RATING - HEAT EXCHANGER ";
o3<< std::setw(4)<< number.substr(0,4); // beware magic numbers
o3<< " = ";
o3<< name;

Thanks! Could I do something slightly different like:

std::string name;
std::string number;
std::string string3 = *** PROCESS RATING - HEAT EXCHANGER ";
string3 += number.setw (4) + " = " + name;

Thanks,
Lynn McGuire
 
L

Lynn McGuire

std::string name;
std::string number;
std::string string3 = *** PROCESS RATING - HEAT EXCHANGER ";
string3 += number.setw (4) + " = " + name;

Nope. But I could use:

std::string name;
std::string number;
while (number.size () < 4)
number = ' ' + number;
std::string string3 = "*** PROCESS RATING - HEAT EXCHANGER ";
string3 += number + " = " + name;

Thanks,
Lynn
 
L

LR

Lynn said:
Nope. But I could use:

std::string name;
std::string number;
while (number.size () < 4)
number = ' ' + number;
std::string string3 = "*** PROCESS RATING - HEAT EXCHANGER ";
string3 += number + " = " + name;

Yes, but if number.size() > 4 to begin with you'll get more than 4
characters from number in string3.

LR
 
R

Richard

[Please do not mail me a copy of your followup]

Lynn McGuire <[email protected]> spake the secret code
Is there a good way to convert this sprintf call to C++ code with
type safety ? The %4.4s is critical to the formatting.

std::string name;
std::string number;
char string3 [100];
sprintf_s (string3, sizeof (string3), "*** PROCESS RATING - HEAT
EXCHANGER %4.4s = %s",
number.c_str (), name.c_str ());

If you have a number of these printf-style formatted text string to
convert, you might want to look at boost.format:
<http://www.boost.org/doc/libs/1_43_0/libs/format/index.html>
 
L

Lynn McGuire

std::string name;
Yes, but if number.size()> 4 to begin with you'll get more than 4
characters from number in string3.

Yes, that is true. I am 100% sure that the number string
variable is a 1 to 9999 with no leading or trailing blanks.

Thanks,
Lynn
 
T

Tim H

Yes, that is true.  I am 100% sure that the number string
variable is a 1 to 9999 with no leading or trailing blanks.

Thanks,
Lynn


From a project I use, using boost format to build a safer sprintf()...


#ifndef PP_UTIL_PRINTFXX_H__
#define PP_UTIL_PRINTFXX_H__

#include <string>
#include <iostream>
#include <sstream>
#include <boost/format.hpp>
#include <boost/preprocessor.hpp>

// Note: this is in the top-level namespace.

// convert any ostreamable object to a string
template <typename T>
inline std::string
to_string(const T &val)
{
std::eek:stringstream oss;
oss << val;
return oss.str();
}

// enable simple string manipulations of boost::format
inline std::string &
operator+=(std::string &str, const boost::format &fmt)
{
// return the original lhs
str += to_string(fmt);
return str;
}
inline std::string
operator+(const std::string &str, const boost::format &fmt)
{
// return a new string
return std::string(str + to_string(fmt));
}
inline std::string
operator+(const boost::format &fmt, const std::string &str)
{
// return a new string
return std::string(to_string(fmt) + str);
}
inline bool
operator==(const std::string &str, const boost::format &fmt)
{
return (str == to_string(fmt));
}

inline bool
operator==(const boost::format &fmt, const std::string &str)
{
return (str == fmt);
}
inline bool
operator!=(const std::string &str, const boost::format &fmt)
{
return !(str == fmt);
}
inline bool
operator!=(const boost::format &fmt, const std::string &str)
{
return !(str == fmt);
}

// A lot of this is boost magic to generate a load of template
// definitions. Not pretty, but useful.

#define GEN_ARG(Z, N, _) % BOOST_PP_CAT(a, N)

#define BOOST_PP_LOCAL_MACRO(N) \
template <BOOST_PP_ENUM_PARAMS(N, class T)> \
void \
printfxx(const std::string& fmt, \
BOOST_PP_ENUM_BINARY_PARAMS(N, const T, &a)) { \
std::cout << boost::format(fmt) BOOST_PP_REPEAT(N, GEN_ARG,
_); \
} \
template <BOOST_PP_ENUM_PARAMS(N, class T)> \
void \
fprintfxx(std::eek:stream &out, const std::string& fmt, \
BOOST_PP_ENUM_BINARY_PARAMS(N, const T, &a)) { \
out << boost::format(fmt) BOOST_PP_REPEAT(N, GEN_ARG, _); \
} \
template <BOOST_PP_ENUM_PARAMS(N, class T)> \
std::string \
sprintfxx(const std::string& fmt, \
BOOST_PP_ENUM_BINARY_PARAMS(N, const T, &a)) { \
return to_string(boost::format(fmt) BOOST_PP_REPEAT(N,
GEN_ARG, _)); \
}

// allow up to 20 args
#define BOOST_PP_LOCAL_LIMITS (1, 16)
#include BOOST_PP_LOCAL_ITERATE()
#undef GEN_ARG


// no-args version is easier to write by hand instead of messing with
// BOOST_PP_EXPR_IF
inline void
printfxx(const std::string &fmt)
{
std::cout << boost::format(fmt);
}
inline void
fprintfxx(std::eek:stream &out, const std::string &fmt)
{
out << boost::format(fmt);
}
inline std::string
sprintfxx(const std::string &fmt)
{
return to_string(boost::format(fmt));
}

#endif // PP_UTIL_PRINTFXX_H__
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top