Help reading code

This is a discussion on Help reading code within the C Programming forums, part of the General Programming Boards category; Could someone help me understand what is happening with this code below. I am having trouble understanding stdout, stdin, and ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    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?

    Code:
         /* 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);
            fflush(stdout);
        }
        /* 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);
            fflush(stderr);
        }
        /* append message to logfile */
        else if (strcmp(logfile,"") != 0)
        {
            logfp = fopen(logfile,"a");
            if (!logfp)
            {
                sprintf(jnlmsg,"Unable to open logfile: |%s|\n",logfile);
                ssw_jnlreq(0,"PS",0,jnl.mod,jnl.blk,jnlmsg,0,0);
                return -1;

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    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
    Location
    Katy, Texas
    Posts
    2,309
    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
    Posts
    115

    Thanks for your help

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

    -Carl

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 Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 03: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, 12:33 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21