Hi,
I'm trying to write a function that accepts a string of some kind
(either char * or std::string) that can be passed something in printf
semantics.
For example:
void test( string passedIn )
{
....etc..
}
test( "Whatever: %s = %d", val, 3 );
How would I write the test function to accept such a string so that it
can handle whatever is passed as a single string? (ie so that it will
get "Whatever: val = 3"
Thanks in advance. 5 1559
winbatch wrote: Hi, I'm trying to write a function that accepts a string of some kind (either char * or std::string) that can be passed something in printf semantics.
For example:
void test( string passedIn ) { ...etc.. }
test( "Whatever: %s = %d", val, 3 );
How would I write the test function to accept such a string so that it can handle whatever is passed as a single string? (ie so that it will get "Whatever: val = 3"
Thanks in advance.
If you really want to do this, declare your function like so
void test(std:string, ...);
Then, in the implementation of string, use the macros and data types from
<stdarg.h> (or <cstdarg>) to access the variable argument list.
However, functions with variable argument lists are hard to implement and use
correctly, since the are not type-safe. I'd recommend trying to find another
solution.
Jonathan
I thought of doing it this way, but I thought that the 'variable'
parameters were all of the same datatype as the 'known' parameter. (If
I'm right), this wouldn't work since it could be passed integers, etc.
Anybody have a different idea?
winbatch wrote: I thought of doing it this way, but I thought that the 'variable' parameters were all of the same datatype as the 'known' parameter.
No, they can be any POD type. I think in Java they all have to have the same
type.
(If I'm right), this wouldn't work since it could be passed integers, etc.
Anybody have a different idea?
Another approach is illustrated by Boost.Format: http://www.boost.org/libs/format/index.html
Jonathan
"winbatch" <ho********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com... Hi, I'm trying to write a function that accepts a string of some kind (either char * or std::string) that can be passed something in printf semantics.
For example:
void test( string passedIn ) { ...etc.. }
test( "Whatever: %s = %d", val, 3 );
How would I write the test function to accept such a string so that it can handle whatever is passed as a single string? (ie so that it will get "Whatever: val = 3"
#include <stdarg.h>
#include <stdio.h>
int test (const char* fmt, ...)
{
va_list args;
int count;
va_start (args, fmt);
count = vprintf (fmt, args);
va_end (args);
return count;
}
int main ()
{
test ("Whatever: %s = %d\n", "val", 3);
return 0;
}
vsprintf and vfprintf work similarly.
--
Regards,
Joe Hotchkiss, http://joe.hotchkiss.com http://harrowsubaqua.org.uk
XXXXXXXXXXXXXXXXXXXXXXXXX
X joe.hotchkiss X
X at baesystems.com X
XXXXXXXXXXXXXXXXXXXXXXXXX
Joe,
Thanks for your help. I ended up using vsnprintf which worked out great.
( I wanted to store the converted string and then write out the log file,
not just print it out like vprintf did).
Dan
"Joe Hotchkiss" <no****@baesystems.com> wrote in message
news:42**********@baen1673807.greenlnk.net... "winbatch" <ho********@gmail.com> wrote in message news:11**********************@o13g2000cwo.googlegr oups.com... Hi, I'm trying to write a function that accepts a string of some kind (either char * or std::string) that can be passed something in printf semantics.
For example:
void test( string passedIn ) { ...etc.. }
test( "Whatever: %s = %d", val, 3 );
How would I write the test function to accept such a string so that it can handle whatever is passed as a single string? (ie so that it will get "Whatever: val = 3"
#include <stdarg.h> #include <stdio.h>
int test (const char* fmt, ...) { va_list args; int count;
va_start (args, fmt); count = vprintf (fmt, args); va_end (args);
return count; }
int main () { test ("Whatever: %s = %d\n", "val", 3);
return 0; }
vsprintf and vfprintf work similarly.
-- Regards,
Joe Hotchkiss, http://joe.hotchkiss.com http://harrowsubaqua.org.uk
XXXXXXXXXXXXXXXXXXXXXXXXX X joe.hotchkiss X X at baesystems.com X XXXXXXXXXXXXXXXXXXXXXXXXX This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: sunfiresg |
last post by:
During an interview, I am asked to answer a question:
Printf is a major formatted output function provided by the standard C
library. Printf accepts a formatting string followed by a various...
|
by: Marco Loskamp |
last post by:
Dear list,
I'm trying to dynamically generate functions; it seems that what I
really want is beyond C itself, but I'd like to be confirmed here.
In the minimal example below, I'd like to...
|
by: mohan |
last post by:
Hi All,
How to implement virtual concept in c.
TIA
Mohan
|
by: neilcancer |
last post by:
My English is poor...
There are some sort algorithms which sort a sequencial list. The
sequencial list was defined in "list-seq.h".
sort_and_time() accepts one of these sort functions. It uses...
|
by: benben |
last post by:
I have the following code snippet:
class converter
{
// ...
public:
virtual polar_point convert_to_polar(const point&) = 0;
};
void f(const std::vector<point>& points, converter& cvt)
|
by: Janus |
last post by:
Hi,
Is there a way to pass arguments to the callback function used inside
an addEventListener?
I see that I can only list the name of the callback function.
For eg, I use this:
var...
|
by: ravi |
last post by:
Hello everybody,
I am writing a small application which does some work before the user
main function starts execution.
I am trying to #define the main function.
But the problem is that,
|
by: Jonathan Green |
last post by:
I have some questions about the behavior of the code below.
#include <stdio.h>
void test(void *v);
int main(void)
{
int i=1;
int j=2;
|
by: riva |
last post by:
This is an interview question from: http://www.freshersworld.com/interview/technical_interview_C.htm
Can You write a function similar to printf() ?
I can only think using putchar() and casting...
|
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...
|
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...
|
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...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
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)...
|
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...
|
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....
|
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
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |