Chinaunix
标题:
有关buffer的问题。
[打印本页]
作者:
lxl_018
时间:
2004-12-03 15:01
标题:
有关buffer的问题。
目的:利用Stand I/O写一个30M的文件,调节单次写的size,观察具体每次消耗的时间。
源程序如下:
#include <stdio.h>;
#include <stdlib.h>;
#define BUFSIZE 64
int filesize=30*1024*1024;
main()
{
FILE *stream;
long total = 0;
int c = 97;
char buffer[BUFSIZE];
if((stream = fopen("test1","w"
) == NULL)
{
printf("Could not open file test.\n"
;
exit(1);
}
setbuf(stream,buffer);
while(total < filesize)
{
putc(c,stream);
total++;
}
printf("total output in test1:%ld \n",total);
exit(0);
}
程序运行后输出为:total output in test1:1633771877
test1文件内容为:aaaaaaaaaaaaaaaaaaaaaaa...eaaa
调试时显示:single stepping until exit from function putc,which has no line number information.
若取消setbuf,则程序运行正常,但不能达到调整buffer大小,观测消耗时间的目的.请问该如何修改本程序?
作者:
aero
时间:
2004-12-03 15:52
标题:
有关buffer的问题。
查了一下man,setbuf函数只能改变buffer的位置而不能改变默认buffer的大小。改变大小和位置要用setbuffer函数。最根本的是使用setvbuf函数。
作者:
lxl_018
时间:
2004-12-04 00:18
标题:
有关buffer的问题。
多谢,用setvbuf替代setbuf后问题解决:
setvbuf(stream,buffer,_IOLBF,BUFSIZE);
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2