use fprintf to write to the console

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • baumann@pan

    use fprintf to write to the console

    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.

  • Richard Bos

    #2
    Re: use fprintf to write to the console

    "baumann@pa n" <baumann.Pan@gm ail.com> wrote:
    [color=blue]
    > 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.[/color]

    You mean, to stderr?
    [color=blue]
    > how can I do so with fprintf since fprintf only accept FILE*? while
    > error std console has int value 2.[/color]

    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

    Comment

    • pete

      #3
      Re: use fprintf to write to the console

      baumann@pan wrote:[color=blue]
      >
      > 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.[/color]

      #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

      Comment

      • CBFalconer

        #4
        Re: use fprintf to write to the console

        "baumann@pa n" wrote:[color=blue]
        >
        > 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.[/color]

        #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(reporter rs);

        --
        Some informative links:
        news:news.annou nce.newusers
        Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!






        Comment

        • Walter Roberson

          #5
          Re: use fprintf to write to the console

          In article <428A17C9.6E774 0E2@yahoo.com>,
          CBFalconer <cbfalconer@wor ldnet.att.net> wrote:[color=blue]
          >#define LOG_TO_FILE 1 /* or 0, or leave undefined */[/color]
          [color=blue]
          > if (LOG_TO_FILE) {[/color]

          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

          Comment

          Working...