Programming cp185
![programming cp185 programming cp185](https://alexnld.com/wp-content/uploads/2015/06/119.jpg)
Like getchar and putchar, getc and putc may be macros instead of functions.
![programming cp185 programming cp185](https://www.radiotronics.us/media/catalog/category/cache/1200_630_motorola-cp185.jpg)
Putc writes the character c to the file fp and returns the character written, or EOF if an error occurs. Getc returns the next character from the stream referred to by fp it returns EOF for end of file or error. getc returns the next character from a file it needs the file pointer to tell it which file. The next thing needed is a way to read or write the file once it is open. (The error can be identified more precisely see the discussion of error-handling functions at the end of Section 1 in Appendix B.) If there is any error, fopen will return NULL. Trying to read a file that does not exist is an error, and there may be other causes of error as well, like trying to read a file when you don't have permission. Opening an existing file for writing causes the old contents to be discarded, while opening for appending preserves them. If a file that does not exist is opened for writing or appending, it is created if possible. Some systems distinguish between text and binary files for the latter, a "b" must be appended to the mode string. Allowable modes include read ( "r"), write ( "w"), and append ( "a"). The second argument is the mode, also a character string, which indicates how one intends to use the file. The first argument of fopen is a character string containing the name of the file. (Details of how fopen can be implemented on the UNIX system are given in Section 8.5.) Notice that FILE is a type name, like int, not a structure tag it is defined with a typedef. This says that fp is a pointer to a FILE, and fopen returns a pointer to a FILE. The only declaration needed for a file pointer is exemplified by Users don't need to know the details, because the definitions obtained from include a structure declaration called FILE. This pointer, called the file pointer, points to a structure that contains information about the file, such as the location of a buffer, the current character position in the buffer, whether the file is being read or written, and whether errors or end of file have occurred. fopen takes an external name like x.c or y.c, does some housekeeping and negotiation with the operating system (details of which needn't concern us), and returns a pointer to be used in subsequent reads or writes of the file. Before it can be read or written, a file has to be opened by the library function fopen.