-
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;
-
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.
-
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.
-
Thanks for your help
Thanks for taking time to help me better understand the code. Your help is greatly appreciated.
-Carl