linux 学习笔记之 Standard I/O Library
Three types of buffereing provided:
- Fully buffered. Files residing on disk are normally fully buffered by the standard I/O library.
- Line buffered. Line buffering is typically used on a system when it refers to a terminal: standard input and standard output.
- Unbuffered. It makes the data output as soon as possible. The standard error stream, for example, is normally unbuffered. This is so that any error messages are displayed as quickly as possible.
void setbuf(FILE *fp, char *buf);
void setvbuf(FILE *fp, char *buf, int mode, size_t size);
- With setbuf, we can turn buffering on or off. To enable buffering, buf must point to a buffer of length BUFSIZ. To disable buffering, we set buf to NULL.
- With setvbuf, We specify exactuly which type of buffering we want. This is done with the mode argument: _IOFBUF -> fully buffered, _IOLBUF -> line buffered, _IONBUF -> unbuffered. The size argument equal the length of the buf.
- In general, we should let the system choose the bufer size and automatically allocate the buffer. When we do this, the standard I/O library automatically release the buffer when we close the stream.
int fflush(FILE *fp)
2. Opening Steam
FILE * freopen(const char *pathname, const char *type, FILE *fp);
FILE * fdopen(int fd, const char *tpye);
- The freopen function opens a specified file on a specified steam, closeing the stream first if it is already open. We usually use it link a standard I/O to a file as freopen("filename","r",stdin) this function equal dup2(fd,0).
- The fdopen function takes an existing file descriptor, which we could obtain form the open, dup, dup2, fcntl, pipe, socket, socketpair or accept functions, and associates a standard I/O steam with the descriptor. The descriptor has already been opened, so opening for write does not truncate the file. Also, The standard I/O append mode cannot create the file.
When a file is opened for reading or writing, the following restrictions apply:
- Output cannot be directly followed by input without an interveing fflush, fseek, fsetops or rewind.
- Input cannot be directly followed by output without an interveing fseek, fsetpos or rewind, or an input opereation that encounters and end of file.
- Note that if a new file is created by specifying a type of either w or a, we are not able to specify the file's access permission bits, as we were able to de witch the open function and the create function.
3. Reading and Writing a Stream
Once we open a stream, we can choose form among three types of unformatted I/O:
- Character-at-a-time I/O
int getc(FILE *fp);
int fgetc(FILE *fp);
- Line-at-a-time I/O
char * fgets(char *buf, int n, FILE * fp);
char * fputs(const char *buf, FILE *fp);
- Direct I/O
size_t fread(void * ptr, size_t size, size_t nobj, FILE *fp);
size_t fwrite(const void *fp, sizt_t size, size_t nobj, FILE *fp);
4. Implementation Details
Each standard I/O steam has a associated file descriptor, and we can obtain the descriptor for a stream by calling fileno.