use fprintf to write to the console

B

baumann@pan

hi all,

i want to use the fprintf to make log.

if defined LOG_TO_FILE, it is easy to use fprintf to write the log
file.

if not defined LOG_TO_FILE, i want to simply write to error std
console.


how can I do so with fprintf since fprintf only accept FILE*? while
error std console has int value 2.
 
R

Richard Bos

baumann@pan said:
if defined LOG_TO_FILE, it is easy to use fprintf to write the log
file.

if not defined LOG_TO_FILE, i want to simply write to error std
console.

You mean, to stderr?
how can I do so with fprintf since fprintf only accept FILE*? while
error std console has int value 2.

There is no "error std console" with any int value, even 2, in ISO C.
You may be thinking of POSIX.

stderr is a FILE *.

Richard
 
P

pete

baumann@pan said:
hi all,

i want to use the fprintf to make log.

if defined LOG_TO_FILE, it is easy to use fprintf to write the log
file.

if not defined LOG_TO_FILE, i want to simply write to error std
console.

how can I do so with fprintf since fprintf only accept FILE*? while
error std console has int value 2.

#include <stdio.h>

int main(void)
{
fprintf(stderr, "%s",
"if not defined LOG_TO_FILE, "
"i want to simply write to error std console.\n"
"how can I do so with fprintf since fprintf only "
"accept FILE*?\n"
"whileerror std console has int value 2.\n"
);
return 0;
}
 
C

CBFalconer

baumann@pan said:
i want to use the fprintf to make log.

if defined LOG_TO_FILE, it is easy to use fprintf to write the log
file.

if not defined LOG_TO_FILE, i want to simply write to error std
console.

how can I do so with fprintf since fprintf only accept FILE*? while
error std console has int value 2.

#define LOG_TO_FILE 1 /* or 0, or leave undefined */
.....
FILE *reporterrs;
.....
if (LOG_TO_FILE) {
reporterrs = fopen(......);
}
else reporterrs = stderr;
.....
/* use reporterrs */
if (LOG_TO_FILE) fclose(reporterrs);

--
Some informative links:
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
 
W

Walter Roberson

#define LOG_TO_FILE 1 /* or 0, or leave undefined */
if (LOG_TO_FILE) {

Small slip there: if you leave LOG_TO_FILE undefined, then in the
if statement, it is going to be treated as an undefined variable
with compiler-dependant results.

I suspect it momentarily slipped your mind that treating
an undefined macro as the value 0 only occurs while evaluating
preprocessing conditional expressions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top