Mind map
Chapter 7 - Input and Output

Map content
Chapter 7 - Input and Output
Standard Input and Output
- As we said in Chapter 1, the library implements a simple model of text input and output. A text stream consists of a sequence of lines; each line ends with a newline character. If the system doesn't operate that way, the library does whatever necessary to make it appear as if it does. For instance, the library might convert carriage return and linefeed to newline on input and back again on output.
Formatted Output - printf
- The output function printf translates internal values to characters. We have used printf informally in previous chapters. The description here covers most typical uses but is not complete; for the full story, see Appendix B. int printf(char *format, arg1, arg2, ...); printf converts, formats, and prints its arguments on the standard output under control of the format. It returns the number of characters printed.
- The output function printf translates internal values to characters. We have used printf informally in previous chapters. The description here covers most typical uses but is not complete; for the full story, see Appendix B. int printf(char *format, arg1, arg2, ...); printf converts, formats, and prints its arguments on the standard output under control of the format. It returns the number of characters printed.
Variable-length Argument Lists
- This section contains an implementation of a minimal version of printf, to show how to write a function that processes a variable-length argument list in a portable way The type va_list is used to declare a variable that will refer to each argument in turn; in minprintf, this variable is called ap, for ``argument pointer.'' The macro va_start initializes ap to point to the first unnamed argument. It must be called once before ap is used. There must be at least one named argument; the final named argument is used by va_start to get started.
Formatted Input - Scanf
- The function scanf is the input analog of printf, providing many of the same conversion facilities in the opposite direction. int scanf(char *format, ...) scanf reads characters from the standard input, interprets them according to the specification in format, and stores the results through the remaining arguments. The format argument is described below; the other arguments, each of which must be a pointer, indicate where the corresponding converted input should be stored.
- The function scanf is the input analog of printf, providing many of the same conversion facilities in the opposite direction. int scanf(char *format, ...) scanf reads characters from the standard input, interprets them according to the specification in format, and stores the results through the remaining arguments. The format argument is described below; the other arguments, each of which must be a pointer, indicate where the corresponding converted input should be stored.
File Access
- The examples so far have all read the standard input and written the standard output, which are automatically defined for a program by the local operating system. The next step is to write a program that accesses a file that is not already connected to the program. One program that illustrates the need for such operations is cat, which concatenates a set of named files into the standard output
Error
- Handling - Stderr and Exit The treatment of errors in cat is not ideal. The trouble is that if one of the files can't be accessed for some reason, the diagnostic is printed at the end of the concatenated output. That might be acceptable if the output is going to a screen, but not if it's going into a file or into another program via a pipeline.
Line Input and Output
- The standard library provides an input and output routine fgets that is similar to the getline function that we have used in earlier chapters: char *fgets(char *line, int maxline, FILE *fp) fgets reads the next input line (including the newline) from file fp into the character array line; at most maxline-1 characters will be read. The resulting line is terminated with '\0'. Normally fgets returns line; on end of file or error it returns NULL. (Our getline returns the line length, which is a more useful value; zero means end of file.)
Images and files in the map
Information
- Description
- Mind Map on Chapter 7 - Input and Output, created by juan manuel figueroa solis on 07/11/2018.
- Created by
- juan manuel figueroa solis
- Views
- 0
- Rating
More on Input, Output and File Handling
