473,320 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

My own sprintf(...)

Hi,

I have default const strings that we use as templates to send emails, (and
multi language messages).
We also want to allow the users to format their own messages.

We have specific messages like devices name and certain settings.

I can use sprintf(...) to replace numbers or strings, but how can I replace
my own variables?

for example

const char * szMsg = "#device# was turned on at #time#";

std::string MyFormat( const char * s )
{
std::string szRet = "";
// replace all the #device# with "device number x"
// replace #time# with "12:02:36pm"
// ...and so on...

return szRet;
}

Does the standard have a way of doing search and replace?

Many thanks

Simon
Aug 27 '05 #1
1 1718
"Simon" <sp********@example.com> wrote in message
news:3n***********@individual.net...
for example

const char * szMsg = "#device# was turned on at #time#";

std::string MyFormat( const char * s )
{
std::string szRet = "";
// replace all the #device# with "device number x"
// replace #time# with "12:02:36pm"
// ...and so on...

return szRet;
}
First of all, you may want to take a look at the
boost::format library - it might suit your needs
(although with a different syntax/approach):
http://www.boost.org/libs/format/doc/format.html

Does the standard have a way of doing search and replace?


Almost - it does help to use some wrapper functions.
My personal toolbox includes:

bool replaceFirst(std::string& ioStr, std::string const& findStr,
std::string const& replStr)
{
std::string::size_type const pos = ioStr.find(findStr);
if( std::string::npos == pos )
return false;

ioStr.replace( pos, findStr.size(), replStr );
return true;
}

int replaceAll(std::string& ioStr, std::string const& findStr, std::string
const& replStr)
{
int hits = 0;
for( std::string::size_type pos = 0
; std::string::npos != ( pos = ioStr.find( findStr, pos ) )
; pos += findStr.size()
)
{
ioStr.replace( pos, findStr.size(), replStr );
++hits;
}
return hits;
}
I hope this helps,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Aug 27 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Yodai | last post by:
Hi all.... I have a little problem that's driving me nuts. I can't seem to make any sense of it. I have this small webserver that substitutes some data from a page when finds a substitution...
3
by: huey_jiang | last post by:
Hi All, I am trying to figure out a right syntax to convert an integer array into hex array. sprintf worked for me on doing single integer: int i, Iarray, n=15; char buf; sprintf(buf,...
6
by: jt | last post by:
I need to produce 1 character array from 3 others. I tried sprintf and it terminates on the first 0, null, 0x00 it sees in tmp data. All 3 args print out nice by themselves. By trying to make...
1
by: jimjim | last post by:
Hello, I was wondering about the implications of giving as an argument to sprintf a different data type from the one specified in the format argument. This type of question along with some...
2
by: aap | last post by:
I have the following code #define MAX 32 struct A { char carr; int iarr; int i; }; void main() {
9
by: Neal Barney | last post by:
I have a C program which runs on a device using a Zilog Z180 microprocessor. While it can address 1MB of RAM, it can only address 64KB at any given time. And of that only 16KB can be used for...
12
by: Henryk | last post by:
Hey there, I have some problems with the following code snippet on a Virtex-4 PowerPC with a GCC based compiler char chData; sprintf(&chData, "%+05.0f", -0.038f); --I get "-000" ???...
15
by: krister | last post by:
Hello, I'm working in a quite large system that has some limitations. One of those is that I can't use printf() to get an output on a screen. I'm forced to use a special function, let's call it...
5
by: Dave | last post by:
Hi, In awk I can do this: var1="x"; temp = sprintf("Variable 1: %s Variable 2: %%s", var1); # now the value of temp is "Variable 1: x Variable 2: %s" var2="y"; printf(temp,var2);
3
by: google | last post by:
Consider the following code: char str; char str2; strcpy(str, "%alfa% %beta% d%100%d %gamma% %delta%"); printf("printf: "); printf("1%s2", str); printf("\nsprintf: "); sprintf(str2, "1%s2",...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.