写文本文件c ++的有效方式 [英] Effective way to write text file c++

查看:112
本文介绍了写文本文件c ++的有效方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用最少的时间写入大型文本文件。首先,我必须用空白空间初始化文件。我的做法是使一个字符数组,例如250bytes,然后将其复制到文件。我认为这不是很有效的方法。任何更好的主意?

I am trying to write large text files with minimum consumption of time. First of all i have to initialize a file with empty space. My approach is to make a char array of say 250bytes and then copying it into file. I think this isn't much effective way. Any better idea?

char buff[250];
ofstream ofile;
ofile.open("abc.txt");
for(long int i=0;i<20970;i++)
{
  ofile<<buffer;
  ofile<<endl;
 }


推荐答案

并写入单个字符:

#include <fstream>

using namespace std;

int main(int argc,char** argv) {
    ofstream o("file") ;
    o.seekp(100 * 1024 * 1024);
    o << '\0';

    return 0;
}

这只有在事先知道生成文件的大小时才有效。

This is only effective if you know in advance the size of the resulting file.

这篇关于写文本文件c ++的有效方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆