Using streams and file descriptors with fprintf and dprintf respectively

ascii-table.c   avg.txt               case-changer.c  env-var-set.c  functions_ver1.c  mph-to-kph.c     output.c  avg-with-garbage.txt  env-var.c       functions_ver2.c  mph-to-kph_v2.c
$gcc output.c -o output
A regular message on stdout
Also a regular message on stdout
An error message on stderr
A regular message, printed to fd 1
An error message, printed to fd 2
$cat output.c 
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>

int main(void)
   printf("A regular message on stdout\n");

   /* Using streams with fprintf() */
   fprintf(stdout, "Also a regular message on " 
   fprintf(stderr, "An error message on stderr\n");

   /* Using file descriptors with dprintf().
    * This requires _POSIX_C_SOURCE 200809L 
    * (man 3 dprintf)*/
   dprintf(1, "A regular message, printed to "
      "fd 1\n");
   dprintf(2, "An error message, printed to "
      "fd 2\n");
   return 0;

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: