一、标准io与文件io的区别
1.缓存区
(这是哪本书里写的来,忘记了。一定要找到!!!贴原图)
标准io的缓存类型(APUE):
全缓存:在填满标准I/O缓冲区后才进行实际的I/O操作;
行缓存:当输入输出中遇到换行符时,标准I/O库执行I/O操作
无缓存:
vi -t FILE 结果如下:
typedef struct _IO_FILE FILE;
Ctrl + ] 追索源码如下:
struct _IO_FILE {
int _flags; /* High-order word is _IO_MAGIC; rest is flags. */
#define _IO_file_flags _flags
/* The following pointers correspond to the C++ streambuf protocol. */
/* Note: Tk uses the _IO_read_ptr and _IO_read_end fields directly. */
char* _IO_read_ptr; /* Current read pointer */
char* _IO_read_end; /* End of get area. */
char* _IO_read_base; /* Start of putback+get area. */
char* _IO_write_base; /* Start of put area. */
char* _IO_write_ptr; /* Current put pointer. */
char* _IO_write_end; /* End of put area. */
char* _IO_buf_base; /* Start of reserve area. */
char* _IO_buf_end; /* End of reserve area. */
/* The following fields are used to support backing up and undo. */
char *_IO_save_base; /* Pointer to start of non-current get area. */
char *_IO_backup_base; /* Pointer to first valid character of backup area */
char *_IO_save_end; /* Pointer to end of non-current get area. */
struct _IO_marker *_markers;
struct _IO_FILE *_chain;
int _fileno;
#if 0
int _blksize;
#else
int _flags2;
#endif
_IO_off_t _old_offset; /* This used to be _offset but it's too small. */
#define __HAVE_COLUMN /* temporary */
/* 1+column number of pbase(); 0 is unknown. */
unsigned short _cur_column;
signed char _vtable_offset;
char _shortbuf[1];
/* char* _save_gptr; char* _save_egptr; */
_IO_lock_t *_lock;
#ifdef _IO_USE_OLD_IO_FILE
};
二、标准io与文件io的相关函数
三、read和write何时阻塞?何时非阻塞?
默认情况:
默认阻塞:
1.读取终端字符;
2.读取网络socket套接字;
3.管道文件(pipe/fifo);
默认非阻塞:
1.读取磁盘上的文件;
fcntl函数:可以获取、设置文件的属性,设置阻塞或者非阻塞;