5.1 Introduction
5.2 Streams and FILE objects
5.3 Standard input, standard output and standard error
- These three standard I/O streams are referenced by the predefined file pointers stdin, stdout, and stderr. These three file pointers are also defined in the header file <stdio.h>
5.4 Caching
cache type | cache description |
---|---|
full cache | The actual I/O operation is performed when the standard I/O cache is filled (generally used for files placed on disk) |
row cache | The standard I/O library performs I/O operations when newline characters are encountered in input and output |
without cache | The standard I/O library does not buffer characters (the standard error stream stderr is usually not buffered) |
- Standard input and standard output are fully buffered if and only if they do not refer to the interactive device
- Standard error is never fully buffered
Function to change cache type: 0 means success, non-zero means failure
#include <stdio.h>
void setbuf(FILE *fp, char *buf);
int setvbuf(FILE *fp, char *buf, int mode, size_t size);
#include <stdio.h>
int fflush(FILE* fp);
The fflush function can force a stream to be flushed
5.5 Opening a stream
#include <stdio.h>
FILE *fopen(const char *pathname, const char *type);
FILE *freopen(const char *pathname, const char *type, FILE *fp);
FILE *fdopen(int filedes, const char *type);
5.6 Reading and writing streams
- I/O one character at a time: use the fgetc and fputc functions
- I/O one line at a time: use fgets and fputs to read or write one line at a time, each line terminated by a newline character;
- Direct I/O: The fread and fwrite functions support this type of I/O;
5.6.1 Input functions
-
The following three functions can be used to read one character at a time: successfully return the next character, read to the end or error is EOF
- The parameter of getc should not be an expression with side effects (a side effect means that the execution of the expression will affect the parameters in the expression)
- Because fgetc must be a function, you can get the device address
- Calling fgetc will most likely take longer than calling getc
#include <stdio.h>
int getc(FILE *fp);
int fgetc(FILE *fp);
int getchar(void);
- Calling the ferror or feof function can distinguish between an error or the end of the file: if the condition is true, return non-zero, otherwise return 0
#include <stdio.h>
int ferror(FILE *fp);
int feof(FILE *fp);
void clearerr(FILE *fp);
- The clearerr function clears two flags
- After reading from a stream, you can call ungetc to send characters back to the stream
#include <stdio.h>
int ungetc(int c, FILE *fp);
5.6.2 Output function
- Each of the following functions can output a character: corresponding to the input function
#include <stdio.h>
int putc(int c, FILE *fp);
int fpuc(int c, FILE *fp);
int putchar(int c);
5.7 I/O one row at a time
#include <stdio.h>
char *fgets(char *buf, int n, FILE *fp);
char *gets(char *buf);
- gets reads from standard input, while fgets reads from the specified stream
- fgets, must specify the length of the cache n
- gets is a deprecated function
#include <stdio.h>
int fputs(const char *str, FILE *fp);
int puts(const char *str);
- The fputs function writes a string terminated by a null character to the specified stream, and the null character is not written out; note: this does not necessarily output one line at a time;
- puts writes a null-terminated string to standard output, without the terminator; however, puts then writes a newline character to standard output;
- Puts is also best not to use;
5.8 Efficiency of Standard I/O
5.9 Binary I/O
Functions that perform binary I/O operations:
#include <stdio.h>
size_t fread(void *ptr, size_t size, size_t nobj, FILE* fp);
size_t fwrite(const void *ptr, size_t size, size_t nobj, FILE *fp);
5.10 Positioning Streams
- ftell and fseek
- fgetpos and fsetpos: applications that need to run on non-unix systems should use this set of functions
#include <stdio.h>
long ftell(FILE *fp); 成功返回当前位置,出错返回-1
int fseek(FILE *fp, long offset,int whence); 成功返回0,出错返回非0
void rewind(FILE *fp);
int fgetpos(FILE *fp, fpos_t *pos);
int fsetpos(FILE *fp, const fpos_t *pos);
5.11 Formatted I/O
5.11.1 Formatted output
#include <stdio.h>
int printf(const char *format, ...);
int fprintf(FILE *fp, const char *format, ...);
两个函数返回:若成功则为输出字符数,若输出出错则为负值
int sprintf(char *buf, const char *format, ...);
返回:存入数组的字符数
5.11.2 Formatted input
#include <stdio.h>
int scanf(const char *format, ...);
int fscanf(FILE *fp, const char *format, ...);
int sscanf(const char *buf, const char *format, ...);
5.12 Implementation Details
Each I/O stream has a file descriptor associated with it, you can call fileno on a stream to get its descriptor
#include <stdio.h>
int fileno(FILE *pf);