Page 126 - Programming Microcontrollers in C
P. 126

Input and Output    111

                          is the standard input, and defaults to the keyboard. stdout in the
                          standard output and defaults to the terminal screen, and stderr is
                          the standard error output which defaults to the terminal screen. These
                          defaults can be redirected by the operating system through the use of
                          redirection operators and pipe operators when the program is executed.
                              The functions getchar() and putchar() are macro defini­
                          tions:
                   #define getchar() getc(stdin)
                   #define putchar(c) putc(c,stdout)

                              Therefore, getchar() will read a single character from the key­
                          board, and putchar()will put a single character to the terminal screen.
                              There are string I/O with the file functions. A string can be put to
                          a file by

                   int fputs(char* string, FILE* fp);
                              This function returns an EOF if an error occurs. Otherwise, it
                          returns a zero when there is no error.
                              A string can be read from a file by
                   int fgets(char* string, int maxline, FILE* fp);

                          fgets returns the next input line from the file fp. These data are
                          written into string, and can contain at most maxline-1 charac­
                          ters. The string is 0 terminated. fgets will return a zero when an
                          error occurs. If there is no error, fgets returns the pointer string.
                              Another important file access function is

                   int fclose(FILE* fp);
                              This function releases the file pointer fp and it causes any data
                          written to the file, but not written to the final destination, to be written.
                          For example, most disk file systems use a buffer to store up a signifi­
                          cant amount of data before it is written to the disk. If data are buffered
                          when the fclose() routine is executed, any buffered data is written
                          to the disk and the program connection to the file system is dissolved.
                              Different compilers have several file handling routines. To ob­
                          tain the maximum advantage of these routines, you should consult
                          the manual that comes with your specific compiler.
                              We have seen several instances of input/output functions. The
                          most common is the printf() function. This function is but one
   121   122   123   124   125   126   127   128   129   130   131