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

fprintf/variable args question

I have code with stuff like this all over it:

sprintf(errmsg,"somefunc(): %s has illegal character
%c",somestring,somechar);
fatal_error(errmsg);

where fatal_error() just fprintf's to stderr and exits. I'd like to
change the above to something like:

fatal_error("somefunc(): %s has illegal character
%c",somestring,somechar);

to get rid of the sprintf, errmsg variable, etc.

However, in fatal_error, would I have to redo all the work of fprintf
(the parsing of the format and the variables)? If fatal_error's
prototype looks like this:

void fatal_error (const char *format, ...);

is there an easy way to pass the args to fprintf()? So I could do
something like this:

void fatal_error (const char *format, ...) {
fprintf(stderr, format, <some magic to send all the args>);
exit (1);
}

? I realize the code for fprintf is pretty easily available and I
could just copy it, but that doesn't seem right.

Thanks,

-Drew
Nov 14 '05 #1
3 3134
Andrew Fabbro wrote:
I have code with stuff like this all over it:

sprintf(errmsg,"somefunc(): %s has illegal character
%c",somestring,somechar);
fatal_error(errmsg);

where fatal_error() just fprintf's to stderr and exits. I'd like to
change the above to something like:

fatal_error("somefunc(): %s has illegal character
%c",somestring,somechar);

to get rid of the sprintf, errmsg variable, etc.

However, in fatal_error, would I have to redo all the work of fprintf
(the parsing of the format and the variables)? If fatal_error's
prototype looks like this:

void fatal_error (const char *format, ...);

is there an easy way to pass the args to fprintf()? [...]


No. Use vfprintf() instead.

--
Er*********@sun.com

Nov 14 '05 #2
Andrew Fabbro wrote:
...
is there an easy way to pass the args to fprintf()? So I could do
something like this:

void fatal_error (const char *format, ...) {
fprintf(stderr, format, <some magic to send all the args>);
exit (1);
}

? I realize the code for fprintf is pretty easily available and I
could just copy it, but that doesn't seem right.
...


void fatal_error (const char *format, ...) {
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
exit (1);
}

Note the use of 'vfprintf' instead of 'fprintf'.

--
Best regards,
Andrey Tarasevich

Nov 14 '05 #3
I was embarassed to find the answer in FAQ answer 15.5.

I will now go to my room without supper.

-Drew
Nov 14 '05 #4

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

Similar topics

10
by: Jack | last post by:
Hello, <%@ Language=VBScript %> <% Response.Write "<FORM name=form1>" dim x Response.Write "<select name=""select1"">" Response.Write "<option value=1>One</option>" Response.Write "<option...
3
by: Bryan Parkoff | last post by:
.....I try to reduce un-necessary temporal variables so it can be optimized for best performance. I try to assign only one register storage so two variables can access to only one register storage...
12
by: Mal Ice | last post by:
I am creating an initial index.htm page on which I show some disclaimers and introduction information. In the head section I have Javascript which determines the screen resolution of the client....
6
by: arcticool | last post by:
Fields vs. Local Variables- as I understand a Field (declared inside a class) can share data across methods in the class, and to other classis (if declared public) where local variables (declared...
2
by: Bit byte | last post by:
I have a C function that takes variable args, i.e. is of the form : foo( const char*, const int, ... ) ; I want to expose this function so that I can call it from VB(6). My questions are:...
2
by: mattdaddym | last post by:
Hi, I have a variable question in regards to my asp .net page. I need to declare a variable whose value is readable/writable to all of the subroutines of a specific page. So far I have one of two...
1
by: Steven W. Orr | last post by:
I have a structure I need to pack. I call struct.pack about a dozen times and each call takes about 53 arguments. I create a sequence of the arguments: a1 = 1 a2 = 2 a3 = 3 etc... a54 = 88...
1
by: globalrev | last post by:
http://mail.python.org/pipermail/python-list/2003-October/233435.html why isnt it printing a in the second(second here, last one in OP) example before complaining? def run(): a = 1 def...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.