use fprintf to write to the console

Discussion in 'C Programming' started by baumann@pan, May 17, 2005.

  1. baumann@pan

    baumann@pan Guest

    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.
     
    baumann@pan, May 17, 2005
    #1
    1. Advertising

  2. baumann@pan

    Richard Bos Guest

    "baumann@pan" <> wrote:

    > 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
     
    Richard Bos, May 17, 2005
    #2
    1. Advertising

  3. baumann@pan

    pete Guest

    baumann@pan wrote:
    >
    > 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;
    }

    --
    pete
     
    pete, May 17, 2005
    #3
  4. baumann@pan

    CBFalconer Guest

    "baumann@pan" wrote:
    >
    > 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:
    news:news.announce.newusers
    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
     
    CBFalconer, May 17, 2005
    #4
  5. In article <>,
    CBFalconer <> wrote:
    >#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.
    --
    "Who Leads?" / "The men who must... driven men, compelled men."
    "Freak men."
    "You're all freaks, sir. But you always have been freaks.
    Life is a freak. That's its hope and glory." -- Alfred Bester, TSMD
     
    Walter Roberson, May 17, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Hansen
    Replies:
    1
    Views:
    386
    Joona I Palaste
    Aug 24, 2003
  2. Peter Hansen
    Replies:
    1
    Views:
    418
    Joona I Palaste
    Aug 24, 2003
  3. L. Westmeier

    When to use "perror" and "fprintf"

    L. Westmeier, May 4, 2004, in forum: C Programming
    Replies:
    6
    Views:
    742
    OSHIMA
    May 13, 2004
  4. G Patel
    Replies:
    17
    Views:
    1,057
    Vesa Siivola
    Feb 14, 2005
  5. the.mago
    Replies:
    1
    Views:
    459
    Gregory Currie
    Sep 26, 2006
Loading...

Share This Page