得到fprintf的系統緩存方法

// 得到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;

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章