473,386 Members | 1,786 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,386 software developers and data experts.

wrapper printf function

Hi,

I need a 'wrapper' function for printf. So that anywhere in my program
if i call the wrapper function it does the same thing as the regular
printf function, but puts a custom message at the begining.

so here it goes :-

int my_printf ( <what do I put here> )
{
printf("This is My Header::");
printf( <Pass the arguments of the my_printf right here>);
}

and in some where in my main pgm files;

my_printf("This is a test %d, %x, %s.\n", 10, 10, "10");

should do exactly as printf does, except put "This is My Header::" at
the begining.

Please help!
Thanks
Nov 13 '05 #1
2 14013
rejeesh wrote:

Hi,

I need a 'wrapper' function for printf. So that anywhere in my program
if i call the wrapper function it does the same thing as the regular
printf function, but puts a custom message at the begining.

so here it goes :-

int my_printf ( <what do I put here> )
{
printf("This is My Header::");
printf( <Pass the arguments of the my_printf right here>);
}

and in some where in my main pgm files;

my_printf("This is a test %d, %x, %s.\n", 10, 10, "10");

should do exactly as printf does, except put "This is My Header::" at
the begining.


#include <stdio.h>
#include <stdarg.h>
int my_printf(const char *format, ...) {
int res1, res2;
va_list ap;
res1 = printf("This is My Header::");
if (res1 < 0)
return res1;
va_start (ap, format);
res2 = vprintf(format, ap);
va_end (ap);
return (res2 < 0) ? res2 : res1 + res2;
}

--
Er*********@sun.com
Nov 13 '05 #2
re*****@tenesix.com (rejeesh) writes:
I need a 'wrapper' function for printf. So that anywhere in my program
if i call the wrapper function it does the same thing as the regular
printf function, but puts a custom message at the begining.


This is a FAQ.

15.5: How can I write a function that takes a format string and a
variable number of arguments, like printf(), and passes them to
printf() to do most of the work?

A: Use vprintf(), vfprintf(), or vsprintf().

Here is an error() function which prints an error message,
preceded by the string "error: " and terminated with a newline:

#include <stdio.h>
#include <stdarg.h>

void error(char *fmt, ...)
{
va_list argp;
fprintf(stderr, "error: ");
va_start(argp, fmt);
vfprintf(stderr, fmt, argp);
va_end(argp);
fprintf(stderr, "\n");
}

See also question 15.7.

References: K&R2 Sec. 8.3 p. 174, Sec. B1.2 p. 245; ISO
Secs. 7.9.6.7,7.9.6.8,7.9.6.9; H&S Sec. 15.12 pp. 379-80; PCS
Sec. 11 pp. 186-7.

--
"To get the best out of this book, I strongly recommend that you read it."
--Richard Heathfield
Nov 13 '05 #3

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

Similar topics

2
by: James S | last post by:
Hi, Basically I've been fighting with this code for a few days now and can't seem to work around this problem. Included is the output, the program I use to get this error and the source code for...
12
by: Egil M?ller | last post by:
Is there any way to create transparent wrapper objects in Python? I thought implementing __getattribute__ on either the wrapper class or its metaclass would do the trick, but it does not work for...
0
by: Nilsson Mats | last post by:
Hi! I have an intresting problem for our programming community on Solaris. I want to develop an environment where: 1) The developers shouldn't need to bother about which Perl version to use....
2
by: selder21 | last post by:
Hi, I am trying to make a wrapper in c so i can compile a program using c++ code in gcc. The next setup actually works, but when i try to use it with c++ classes i get "undefined reference to...
14
by: Java and Swing | last post by:
static PyObject *wrap_doStuff(PyObject *self, PyObject *args) { // this will store the result in a Python object PyObject *finalResult; // get arguments from Python char *result = 0; char *in=...
3
by: Colin Kern | last post by:
I'm trying to write a function that is a wrapper for printf. It looks something like: myprintf(int myarg1, int myarg2, const char* fmt, ...) { /* Do something with myarg1, myarg2 *.
4
by: peterbe | last post by:
This works exactly as you would expect:: from time import sleep def foo(on='ABC'): for e in list(on): sleep(1) yield e When I run this on the command line It takes about 3 seconds to...
23
by: I.M. !Knuth | last post by:
A while back, I was mucking around with a recursive function. For brevity's sake, I'll define it like this: int func_recurs(int arg1, int arg2, int prev_pos) { int crnt_pos = 0; int result; ...
3
by: markww | last post by:
Hi, I have a wrapper around some 3rd party database library function. The pseudo code looks like the following - it is meant to open a table in a database, extract values from a table, then copy...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.