// 得到fprintf的系统缓存方法
#include <stdio.h>
int main (int argc, char **argv)
{
FILE *fp;
fp = fopen ("xxx", "w");
fprintf (fp, "foo");
printf ("%d\n", fp->_IO_buf_end - fp->_IO_buf_base);
fclose(fp);
return 0;
}
/
//我的系统得到的BUF值是: 4096字节
///
#include <stdio.h>
#include <string.h>
int main (int argc, char **argv)
{
FILE *fp;
fp = fopen ("xxx", "w");
char buf[4096+1] = {0};
memset(buf, 'a', 4095); // 4096 写入
fprintf (fp, buf); // 此时没有写入文件,仅在缓存中 可以sleep(10) , 去文件中看。
fprintf(fp, "1234"); // 此时超过4096个字节,写入文件,但只写到1 ,而234没有写入文件
//printf("buf content: %s\n", fp->_IO_buf_base);
printf ("sysbuf size: %d\n", fp->_IO_buf_end - fp->_IO_buf_base);
while(1);
return 0;
}
///
在/usr/include/stdio.h中
typedef struct _IO_FILE FILE;
在 /usr/include/libio.h中
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
};
/
TC中typedef struct { short level; /* fill/empty level of buffer */ unsigned flags; /* File status flags */ char fd; /* File descriptor */ unsigned char hold; /* Ungetc char if no buffer */ short bsize; /* Buffer size */ unsigned char *buffer; /* Data transfer buffer */ unsigned char *curp; /* Current active pointer */ unsigned istemp; /* Temporary file indicator */ short token; /* Used for validity checking */} FILE; /* This is the FILE object */
VC中在"stdio.h"中有如下定义struct _iobuf { char *_ptr; //文件输入的下一个位置 int _cnt; //当前缓冲区的相对位置 char *_base; //指基础位置(即是文件的其始位置) int _flag; //文件标志 int _file; //文件的有效性验证 int _charbuf; //检查缓冲区状况,如果无缓冲区则不读取 int _bufsiz; //文件的大小 char *_tmpfname; //临时文件名 };typedef struct _iobuf FILE;