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. 6 2457
On Wed, 19 Sep 2007 00:55:35 -0700, "ju**********@yahoo.co.in"
<ju**********@yahoo.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_ptr, format);
vsprintf(text, format, arg_ptr);
va_end(arg_ptr);
if ( fp )
{
fprintf(fp, "%s", text);
}
else
{
fprintf(stdout, "%s", text);
}
}
Regards
--
jay
On Sep 19, 1:35 pm, jaysome <jays...@hotmail.comwrote:
On Wed, 19 Sep 2007 00:55:35 -0700, "junky_fel...@yahoo.co.in"
<junky_fel...@yahoo.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_ptr, 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.
On Wed, 19 Sep 2007 00:55:35 -0700, "ju**********@yahoo.co.in"
<ju**********@yahoo.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
"ju**********@yahoo.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
<ju**********@yahoo.co.inwrote in message
news:11**********************@e9g2000prf.googlegro ups.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
jaysome wrote:
On Wed, 19 Sep 2007 00:55:35 -0700, "ju**********@yahoo.co.in"
<ju**********@yahoo.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_ptr, 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> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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) {...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
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,...
|
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...
|
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,...
| |