setbuf ()- 控制緩衝
格式
#include <stdio.h>
void setbuf(FILE *, char *buffer);語言層次
ANSI
安全執行緒
是
說明
如果作業系統支援使用者定義的緩衝區, setbuf() 會控制所指定 串流的緩衝。 只有在使用整合檔案系統時, setbuf() 函數才在 ILE C 中運作。 在完成任何 I/O 或重新定位之前, stream 指標必須先參照開啟檔案。
如果 buffer 引數是 NULL,則 stream 不會緩衝。 否則, 緩衝 必須指向長度為 BUFSIZ的字元陣列,這是 <stdio.h> 併入檔中定義的緩衝區大小。 對於給定 串流. stdout、 stderr和 stdin 不支援使用者定義緩衝區的預設系統配置緩衝區,系統會使用您指定的 緩衝區作為輸入/輸出緩衝。
setvbuf() 功能比 setbuf() 功能更靈活。
回覆值
此方法不會傳回任何值。
範例
此範例會開啟檔案 setbuf.dat 以進行寫入。 然後,它會呼叫
setbuf() 函數,以建立長度為 BUFSIZ的緩衝區。 將 string 寫入串流時,會使用緩衝區 buf ,並在將其清除至檔案之前包含該字串。#include <stdio.h>
int main(void)
{
char buf[BUFSIZ];
char string[] = "hello world";
FILE *stream;
memset(buf,'\0',BUFSIZ); /* initialize buf to null characters */
stream = fopen("setbuf.dat", "wb");
setbuf(stream,buf); /* set up buffer */
fwrite(string, sizeof(string), 1, stream);
printf("%s\n",buf); /* string is found in buf now */
fclose(stream); /* buffer is flushed out to myfile.dat */
}