Thread: Help reading code

  1. #1
    Registered User
    Join Date
    Apr 2008

    Help reading code

    Could someone help me understand what is happening with this code below. I am having trouble understanding stdout, stdin, and stderr. Are they buffers where data sits until told what to do?

         /* send to standard output */
        if (strcmp(logfile,"stdout") == 0)  compares the strings (file pointers
            fprintf(stdout,"%s %4.4d %d %d %s\n",cur_dt_tm,jnl.blk,jnl.val1,
                jnl.val2, jnl.mesg);
        /* send to standard error */
        else if (strcmp(logfile,"stderr") == 0)
            fprintf(stderr,"%s %4.4d %d %d %s\n",cur_dt_tm,jnl.blk,jnl.val1,
                jnl.val2, jnl.mesg);
        /* append message to logfile */
        else if (strcmp(logfile,"") != 0)
            logfp = fopen(logfile,"a");
            if (!logfp)
                sprintf(jnlmsg,"Unable to open logfile: |%s|\n",logfile);
                return -1;

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Northern Virginia/Washington DC Metropolitan Area
    stdout/stderr/stdin are standard streams.

    In the code, there appears to be a string (character array) called logfile that the code is testing to see if it is equal to "stdout", "stderr" or null. Depending on what the string is, you then write to the appropriate stream.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Katy, Texas
    stdin, stdout and stderr are all predefined "file handles", or "file definitions". The operating system opens them for your program automatically.

    stdin is the default means to obtain input data, whether it is read from the keyboard, a file, or other source.

    stdout is the default means for outputting data, be that to the display, a file, or other destination.

    stderr is the default destination for error messages. It could be the display, a file, or other destination.

    In your code, the logic is looking to see if it is supposed to be logging to stdout or stderrr or another file definition.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  4. #4
    Registered User
    Join Date
    Apr 2008

    Thanks for your help

    Thanks for taking time to help me better understand the code. Your help is greatly appreciated.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  3. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  4. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 01:33 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM