STDIN, STDOUT, STDERR


These three file pointers are automatically defined when a program executes and provide access to the keyboard and screen.

stdin

By default stdin accesses the keyboard. Functions that read stdin include... The following functions can access stdin stdin example program.

stdout

stdout sends data to the screen. Functions that write to stdout include....

stderr

stderr also writes to the screen. If you are using a Unix based system the data sent to stdout and stderr can be seperated and sent to different places. Functions that could write to stderr include...

Top Master Index C Keywords Functions


When working in embedded system, without a standard console, one can get the stdio system working by adding routines to replace the base connectors used by the compiler. These vary from system to system. For example, on GCC, the following work:

//Replace the gcc default connectors for stdin and stdout  
int _write(int file, char *ptr, int len) {  
    int i;  
    file = file;  
    for (i = 0; i < len; i++) {  
        UART_UartPutChar(*ptr++);  
        }  
    return len;  
    }  
  
  
int _read (int fd, const void *buf, size_t count) {  
  size_t CharCnt = 0x00;  
  (void)fd;                            /* Parameter is not used, suppress unused argument warning */  
  for (;count > 0x00; --count) {  
    /* Save character received by UARTx device into the receive buffer */  
    while(!(*(uint8_t*)buf = (unsigned char)UART_UartGetChar()));  
    CharCnt++;                         /* Increase char counter */  
    /* Stop reading if CR (Ox0D) character is received */  
    if (*(uint8_t*)buf == 0x0DU) {     /* New line character (CR) received ? */  
      *(uint8_t*)buf = '\n';           /* Yes, convert LF to '\n' char. */  
      break;                           /* Stop loop and return received char(s) */  
    }  
    buf++;                   /* Increase buffer pointer */  
  }  
  //return 1; /* WRONG! */  
  return CharCnt;  
}  
In this example, UART_UartPutChar and UART_UartGetChar are unique to the processor being used and would be replaced with whatever routine transmits or receives a character via the desired IO channel. E.g. it might be a write to LCD routine or a read from voice input.
If you wish to create file pointers to perform I/O to other devices you should use fopen
Martin Leslie