- Standard Input and output
- Formatted output - printf
- Variable-length argument Lists
- Formatted input - scanf
- File access
- Error handling - stderr and exit
- Line input and output
- Miscellaneous Functions
1. Standard Input and output
The simplest input mechanism is to read one character at a time from the standard input, normally the keyboard, with getchar.
int getchar(void)
getchar returns the next input character each time it is called, or EOF when it encounters end of file.
The function
int putchar(int)
is used for output: putchar(c) puts the character c on the standard output.
check other standard I/O functions in stdio.h
2. Formatted output - printf
printf converts, formats, and prints its arguments on the standard output under control of the format. It returns the number of characters printed.
int printf(char *format, arg1, arg2, ...);
3. Variable-length argument Lists
This section shows how to write a function that processes a variable-length argument list in a portable way.
Our program, minprintf, will process the format string and arguments but will call the real printf to do the format conversions.
void minprintf(char *fmt, ...) { va_list ap; /* points to each unnamed arg in turn */ char *p, *sval; int ival; double dval; va_start(ap, fmt); /* make ap point to 1st unnamed arg */ for (p = fmt; *p; p++) { if (*p != '%') { putchar(*p); continue; } switch (*++p) { case 'd': ival = va_arg(ap, int); printf("%d", ival); break; case 'f': dval = va_arg(ap, double); printf("%f", dval); break; case 's': for (sval = va_arg(ap, char *); *sval; sval++) putchar(*sval); break; default: putchar(*p); break; } } va_end(ap); /* clean up when done */ }
4. Formatted input - scanf
scanf reads characters from the standard input, interprets them according to the specification in format, and stores the results through the remaining arguments.
int scanf(char *format, ...)
sscanf scans the string according to the format in format and stores the resulting values through arg1, arg2, etc. These arguments must be pointers.
int sscanf(char *string, char *format, arg1, arg2, ...)
5. File access
File pointer, FILE *fp, 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.
FILE *fp;
FILE *fopen(char *name, char *mode);
This says that fp is a pointer to a FILE, and fopen returns a pointer to a FILE. Notice that FILE is a type name, like int, not a structure tag; it is defined with a typedef.
Check how to use file I/O functions in stdio.h.
6. Error handling - stderr and exit
Output stream, called stderr, is assigned to a program in the same way that stdin and stdout are. Output written on stderr normally appears on the screen even if the standard output is redirected.
Check more detail in errno.h and error handling here.
In stdlib.h defines how to use exit() function.
Note that within main, return expr is equivalent to exit(expr).
7. Line input and output
The standard library stdio.h provides an input and output routine fgets().
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.
For output, the function fputs writes a string (which need not contain a newline) to a file:
int fputs(char *line, FILE *fp)
It returns EOF if an error occurs, and non-negative otherwise.
The library functions gets and puts are similar to fgets and fputs, but operate on stdin and stdout. Confusingly, gets deletes the terminating '\n', and puts adds it.
8. Miscellaneous Functions
No comments:
Post a Comment