得到fprintf的系統緩衝方法

來源:互聯網
上載者:User

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

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.