如何改变ofstream内部的缓冲区大小?

htty326 2008-08-18 08:15:50
如果我一次性要写入1M大小的数据的话,首先建立一个ofstream的对象,其实在写入的过程中,该ofstream对象首先是将数据输出到其内部的缓冲区里,等内部缓冲区满了才将缓冲区的内容写入磁盘,我想请问:如何修改ofstream内部的缓冲区大小呢?
...全文
1530 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
e_sharp 2008-08-29
  • 打赏
  • 举报
回复
UP
htty326 2008-08-29
  • 打赏
  • 举报
回复
今天做了一个测试,分别用C++中fstream库中的类和C语言中的FILE来复制一个10M大小的文件,结果比较令人感到意外。
C++代码如下:
#include <fstream>
#include <windows.h>
#include <iostream>

int main(int argc, char* argv[])
{

std::ofstream ofile;
std::ifstream ifile;
int BuSize=1024*1024*2;
int t1=0,t2=0;
char* Buffer=new char[BuSize];

ifile.open("D:\\Data.txt",std::ios::in);
ofile.open("D:\\Data2.txt",std::ios::out);

t1=::GetTickCount();
while(!ifile.eof())
{
ifile.read(Buffer,BuSize);
ofile.write(Buffer,ifile.gcount());
}
t2=::GetTickCount();
std::cout<<t2-t1<<std::endl;
ifile.close();
ofile.close();
return 0;
}
运行后,时间为4727毫秒

C语言代码如下:
#include <stdio.h>
#include <iostream>
#include <windows.h>

int main(int argc, char* argv[])
{
FILE *fhr=NULL,*fhw=NULL;
int BuSize=1024*1024*2,DataSize=0;
int t1=0,t2=0;
char* Buffer=new char[BuSize];

fhr=fopen("D:\\Data.txt","r");
fhw=fopen("D:\\Data1.txt","w");
t1=::GetTickCount();

while(!feof(fhr))
{
DataSize=fread(Buffer,sizeof(char),BuSize,fhr);
fwrite(Buffer,sizeof(char),DataSize,fhw);
}

t2=::GetTickCount();
std::cout<<t2-t1<<std::endl;
fclose(fhr);
fclose(fhw);
return 0;
}
运行后,时间为234毫秒

C语言代码的执行时间比C++代码的执行时间快了20倍,难道C++的文件操作效率就那么低嘛?
太乙 2008-08-20
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 htty326 的回复:]
如果是无缓冲的IO,不管每次写入的数据量大小是多少,都要执行一次写磁盘操作,如果每次写入的数据量不大并且需要多次写入的话,无缓冲IO的效率就非常低了……
[/Quote]

可以写个程序,测测自己计算机的最佳大小~
taodm 2008-08-20
  • 打赏
  • 举报
回复
修改ofstream的缓冲并不会显著提高io速度。
ofstream下面调用fwrite等,fwrite又调用os提供的函数。
os提供的函数调用驱动提供的东西,驱动又依赖硬盘自己的内部缓冲(大小及算法)
所以,你还是换个思路吧。
htty326 2008-08-20
  • 打赏
  • 举报
回复
如果是无缓冲的IO,不管每次写入的数据量大小是多少,都要执行一次写磁盘操作,如果每次写入的数据量不大并且需要多次写入的话,无缓冲IO的效率就非常低了……
herman~~ 2008-08-19
  • 打赏
  • 举报
回复
1M的数据并不大
就按6楼的做法就ok
太乙 2008-08-19
  • 打赏
  • 举报
回复
要提高文件读写效率啊?
那就不用缓冲啊~~

网上搜搜无缓冲io!~
htty326 2008-08-19
  • 打赏
  • 举报
回复
那为什么3楼的代码能生成15KB的文件而4楼的代码不能生成1M的文件呢?ofstream对象先是把要写入的数据放入缓冲区,然后再写入磁盘的,我是想修改ofstream对象内部的缓冲区,提高文件的读写效率……请各位赐教!谢谢了!
K行天下 2008-08-18
  • 打赏
  • 举报
回复
主要是你write函数使用有误:
改为:



#include <fstream>

int main(int argc, char* argv[])
{
std::ofstream ofile;
char* data = new char[1024*1024];

ofile.open("shi.dat",std::ios::out | std::ios::binary);
ofile.write(data,1024*1024);//长度为data的长度
ofile.close();

return 0;
}
baihacker 2008-08-18
  • 打赏
  • 举报
回复
int main(int argc, char* argv[]) 
{
std::ofstream ofile;
char str[1024]={0};

ofile.open("D:\\shi.dat",std::ios::out | std::ios::binary);
for (int i = 0; i < 1024; ++i) ofile.write(str,1024);
ofile.close();

return 0;
}
htty326 2008-08-18
  • 打赏
  • 举报
回复
#include <fstream>
#include <string>

int main(int argc, char* argv[])
{
std::ofstream ofile;
std::string str="";

ofile.open("D:\\shi.dat",std::ios::out | std::ios::binary);
ofile.write(str.c_str(),1024*1024);
ofile.close();

return 0;
}

可是这段代码怎么也无法生成1M的文件
htty326 2008-08-18
  • 打赏
  • 举报
回复
#include <fstream>
#include <string>

int main(int argc, char* argv[])
{
std::ofstream ofile;
std::string str="";

ofile.open("D:\\shi.dat",std::ios::out | std::ios::binary);
ofile.write(str.c_str(),1024*15);
ofile.close();

return 0;
}
这段代码执行后能够生成一个15KB的文件
htty326 2008-08-18
  • 打赏
  • 举报
回复
上面的方法尝试过了,没有效果!
baihacker 2008-08-18
  • 打赏
  • 举报
回复
The four ofstream constructors are:

Constructor Description
ofstream() Constructs an ofstream object without opening a file.
ofstream( const char*, int, int ) Contructs an ofstream object, opening the specified file.
ofstream( filedesc ) Constructs an ofstream object that is attached to an open file.
ofstream( filedesc, char*, int ) Constructs an ofstream object that is associated with a filebuf object. The filebuf object is attached to an open file and to a specified reserve area.

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧