473,786 Members | 2,368 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

question related to passing variable number of arguments

Hi,

I want to define a function log_file(), that would print the
formatted message to stdout if the FILE pointer passed is NULL else
print the formatted output to the file corresponding to FILE*.

For eg.

extern FILE *fp;

int main(void)
{
int n1=0;
int n2=100;

log_file(fp, "SOME MESSAGE n1=%d n2=%d\n", n1, n2);
}

I want that, log_file() should print the formatted output to stdout,
in case fp is NULL, else
print the output to the corresponding file.
Can some one please help ? I tried my best, but could not find any
solution.

thanks a lot for any help.

Sep 19 '07 #1
6 2478
On Wed, 19 Sep 2007 00:55:35 -0700, "ju**********@y ahoo.co.in"
<ju**********@y ahoo.co.inwrote :
>Hi,

I want to define a function log_file(), that would print the
formatted message to stdout if the FILE pointer passed is NULL else
print the formatted output to the file corresponding to FILE*.

For eg.

extern FILE *fp;

int main(void)
{
int n1=0;
int n2=100;

log_file(fp, "SOME MESSAGE n1=%d n2=%d\n", n1, n2);
}

I want that, log_file() should print the formatted output to stdout,
in case fp is NULL, else
print the output to the corresponding file.
Can some one please help ? I tried my best, but could not find any
solution.

thanks a lot for any help.
Hi junky fellow,

The following doesn't deal with your extern fp, because it represents
a single translation unit that compiles and links. Nevertheless, I
hope it gives you an idea of how you can solve your problem.

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

#define MAX_MSG_LENGTH 256 /*or whatever size you need*/

static FILE *G_fp = NULL;
static void log_file(FILE *fp, const char *format, ...);

int main(void)
{
int n1 = 0;
int n2 = 100;

log_file(G_fp, "SOME MESSAGE n1 = %d and n2 = %d\n", n1, n2);
return 0;
}

static void log_file(FILE *fp, const char *format, ...)
{
va_list arg_ptr;
char text[MAX_MSG_LENGTH + 1];

va_start(arg_pt r, format);
vsprintf(text, format, arg_ptr);
va_end(arg_ptr) ;
if ( fp )
{
fprintf(fp, "%s", text);
}
else
{
fprintf(stdout, "%s", text);
}
}

Regards
--
jay
Sep 19 '07 #2
On Sep 19, 1:35 pm, jaysome <jays...@hotmai l.comwrote:
On Wed, 19 Sep 2007 00:55:35 -0700, "junky_fel...@y ahoo.co.in"

<junky_fel...@y ahoo.co.inwrote :
Hi,
I want to define a function log_file(), that would print the
formatted message to stdout if the FILE pointer passed is NULL else
print the formatted output to the file corresponding to FILE*.
For eg.
extern FILE *fp;
int main(void)
{
int n1=0;
int n2=100;
log_file(fp, "SOME MESSAGE n1=%d n2=%d\n", n1, n2);
}
I want that, log_file() should print the formatted output to stdout,
in case fp is NULL, else
print the output to the corresponding file.
Can some one please help ? I tried my best, but could not find any
solution.
thanks a lot for any help.

Hi junky fellow,

The following doesn't deal with your extern fp, because it represents
a single translation unit that compiles and links. Nevertheless, I
hope it gives you an idea of how you can solve your problem.

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

#define MAX_MSG_LENGTH 256 /*or whatever size you need*/

static FILE *G_fp = NULL;
static void log_file(FILE *fp, const char *format, ...);

int main(void)
{
int n1 = 0;
int n2 = 100;

log_file(G_fp, "SOME MESSAGE n1 = %d and n2 = %d\n", n1, n2);
return 0;

}

static void log_file(FILE *fp, const char *format, ...)
{
va_list arg_ptr;
char text[MAX_MSG_LENGTH + 1];

va_start(arg_pt r, format);
vsprintf(text, format, arg_ptr);
va_end(arg_ptr) ;
if ( fp )
{
fprintf(fp, "%s", text);
}
else
{
fprintf(stdout, "%s", text);
}

}

Thanks a lot, jaysome. This really solved the problem.

Sep 19 '07 #3
On Wed, 19 Sep 2007 00:55:35 -0700, "ju**********@y ahoo.co.in"
<ju**********@y ahoo.co.inwrote :
>Hi,

I want to define a function log_file(), that would print the
formatted message to stdout if the FILE pointer passed is NULL else
print the formatted output to the file corresponding to FILE*.

For eg.

extern FILE *fp;

int main(void)
{
int n1=0;
int n2=100;

log_file(fp, "SOME MESSAGE n1=%d n2=%d\n", n1, n2);
}

I want that, log_file() should print the formatted output to stdout,
in case fp is NULL, else
print the output to the corresponding file.
Can some one please help ? I tried my best, but could not find any
solution.
int logfile(FILE *f, ...){
/* after processing argument list */
if (f == NULL) f = stdout;
return fprintf(...);}

The real issue is what you are going to do with the format string and
the subsequent arguments. Do you really intend to generate your own
code to duplicate the functionality of the *printf functions?

You could make life easy on yourself and use sprintf to perform all
the hard work and then use the much simpler

int logfile(FILE *f, const char *s){
if (f == NULL) f = stdout;
return fputs(s, f);}

At this point, logfile would hardly merit a function and could be
replaced by a macro.
Remove del for email
Sep 19 '07 #4
"ju**********@y ahoo.co.in" wrote:
>
.... snip ...
>
extern FILE *fp;

int main(void) {
int n1=0;
int n2=100;

log_file(fp, "SOME MESSAGE n1=%d n2=%d\n", n1, n2);
}

I want that, log_file() should print the formatted output to stdout,
in case fp is NULL, else print the output to the corresponding file.
Can some one please help ? I tried my best, but could not find any
solution.
In log_file():

if (NULL == fp) fp = stdout;
....

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>
--
Posted via a free Usenet account from http://www.teranews.com

Sep 19 '07 #5

<ju**********@y ahoo.co.inwrote in message
news:11******** **************@ e9g2000prf.goog legroups.com...
Hi,

I want to define a function log_file(), that would print the
formatted message to stdout if the FILE pointer passed is NULL else
print the formatted output to the file corresponding to FILE*.

For eg.

extern FILE *fp;

int main(void)
{
int n1=0;
int n2=100;

log_file(fp, "SOME MESSAGE n1=%d n2=%d\n", n1, n2);
}

I want that, log_file() should print the formatted output to stdout,
in case fp is NULL, else
print the output to the corresponding file.
Can some one please help ? I tried my best, but could not find any
solution.

thanks a lot for any help.
Make it real simple:

#define OUTFILE(f) ((f)?(f):stdout )

then to log:
fprintf( OUTFILE(fp), format, <arguments);

--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Aero Stability and Controls Computing
Sep 19 '07 #6
jaysome wrote:
On Wed, 19 Sep 2007 00:55:35 -0700, "ju**********@y ahoo.co.in"
<ju**********@y ahoo.co.inwrote :
>Hi,

I want to define a function log_file(), that would print the
formatted message to stdout if the FILE pointer passed is NULL else
print the formatted output to the file corresponding to FILE*.

For eg.

extern FILE *fp;

int main(void)
{
int n1=0;
int n2=100;

log_file(fp, "SOME MESSAGE n1=%d n2=%d\n", n1, n2);
}

I want that, log_file() should print the formatted output to stdout,
in case fp is NULL, else
print the output to the corresponding file.
Can some one please help ? I tried my best, but could not find any
solution.

thanks a lot for any help.

Hi junky fellow,

The following doesn't deal with your extern fp, because it represents
a single translation unit that compiles and links. Nevertheless, I
hope it gives you an idea of how you can solve your problem.

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

#define MAX_MSG_LENGTH 256 /*or whatever size you need*/

static FILE *G_fp = NULL;
static void log_file(FILE *fp, const char *format, ...);

int main(void)
{
int n1 = 0;
int n2 = 100;

log_file(G_fp, "SOME MESSAGE n1 = %d and n2 = %d\n", n1, n2);
return 0;
}

static void log_file(FILE *fp, const char *format, ...)
{
va_list arg_ptr;
char text[MAX_MSG_LENGTH + 1];

va_start(arg_pt r, format);
vsprintf(text, format, arg_ptr);
YABOB... KABOOOM!

Better:

static void vfile_log(FILE *flog, const char *message, va_list argp);

void file_log(FILE *flog, const char *msg, ...)
{
va_list argp;

assert(msg != NULL);

va_start(argp, msg);
vfile_log(flog, msg, argp);
va_end(argp);
}

static void vfile_log(FILE *flog, const char *message, va_list argp)
{
....

if (flog && message)
{
(void) vfprintf(flog, message, argp);
(void) fprintf(flog, "\n");
fflush(flog);
}
....
}

--
Tor <torust [at] online [dot] no>
Sep 19 '07 #7

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

Similar topics

3
14952
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
6
2408
by: Bonge Boo! | last post by:
This has got to be obvious, but I can't make it work. I have a form called with 3 pull down menus. They are linked to a database which generates the values for the <SELECT? Pull-downs. Lets say I have values selected for all three pull down menus. When I change the first "top-level" menu I want to reset both the second and third menus to the "default" state.
39
7664
by: Mike MacSween | last post by:
Just spent a happy 10 mins trying to understand a function I wrote sometime ago. Then remembered that arguments are passed by reference, by default. Does the fact that this slowed me down indicate: a) That I don't know enough b) Passing arguments by ref is bad
4
3629
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's useful to help me to solve some basic problem which I may not perceive before. I appreciate your help, sincerely.
6
3954
by: Max | last post by:
Last time I tried to explain this on another forum it didn't go too well, so I'll try my best and if you know what I'm talking about then please tell me how to do this. I have a class, inside I have some public functions and private variables. Inside the class I also have a declaration of a new form object. One of the functions of the class takes that form object, shows it with showdialog and the basically passes the control to the form...
1
3269
by: Shawn | last post by:
As if it won't be clear enough from my code, I'm pretty new to C programming. This code is being compiled with an ANSI-C compatible compiler for a microcontroller. That part, I believe, will be irrelavent. My syntax is surely where I am going wrong. I'd like to be able to call this routine to read different values from another device. This routine would be called quite simply as follows: void main() {
2
3332
by: Ramashish Baranwal | last post by:
Hi, I need to process few out of a variable number of named arguments in a function and pass the remaining to another function that also takes variable number of named arguments. Consider this simple example, def fun1(**kwargs): print kwargs.keys() def fun2(**kwargs):
10
1050
by: mcl | last post by:
Why can I not the change the value of a variable in another class, when I have passed it via a parameter list. I am sure I am being stupid, but I thought passed objects were Read/ Write eg ------------------------------------------------------------ #!/usr/bin/python
9
5285
by: oldyork90 | last post by:
I'm going thru code and have never seen this before http://www.webreference.com/programming/javascript/mk/column2/3.html Look at function CreateDragContainer() on line 25. It has no arguments defined and depends on a function property named arguments to process its input. I poked around and found this is deprecated. How do you pass an unknown number of arguments to a function? Put them in an array?
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9491
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10357
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9959
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4063
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.