#include <stdio.h>
int main ()
{
char buffer[BUFSIZ];
FILE *pFile1, *pFile2;
pFile1=fopen ("D:\\a.txt","w");
pFile2=fopen ("D:\\b.txt","a");
//setbuf ( pFile1 , buffer );
fputs ("This is sent to a buffered stream",pFile1);
fflush (pFile1);
setbuf ( pFile2 , NULL );
fputs ("This is sent to an unbuffered stream",pFile2);
fputs ("dffdgfdgf",pFile1);
fputs ("tydfg",pFile1);
buffer[0]='9';
buffer[1]='3';
buffer[2]='6';
fclose (pFile1);
fclose (pFile2);
return 0;
int main ()
{
char buffer[BUFSIZ];
FILE *pFile1, *pFile2;
pFile1=fopen ("D:\\a.txt","w");
pFile2=fopen ("D:\\b.txt","a");
//setbuf ( pFile1 , buffer );
fputs ("This is sent to a buffered stream",pFile1);
fflush (pFile1);
setbuf ( pFile2 , NULL );
fputs ("This is sent to an unbuffered stream",pFile2);
fputs ("dffdgfdgf",pFile1);
fputs ("tydfg",pFile1);
buffer[0]='9';
buffer[1]='3';
buffer[2]='6';
fclose (pFile1);
fclose (pFile2);
return 0;
}
其中File::File::_ptr File::_base 两个指针很重要,
1:有 setbuf ( pFile1 , buffer );
pFIle1._base=buffer
这里面其中FIle::ptr很重要 fflush(pFile1)就会将File::_base写到File::_ptr中, 文件就会写入一次
2:有 setbuf ( pFile1 , buffer );
pFIle1._base=&"This is sent to a buffered stream";
why:有了setbuf之后我们可以对写入文件的内容可以很容易的修改,拼接,字符串当我们写完了之后内存就不会发生变化,我们用buffer替代了字符串,有封装了一层