Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用c+;创建大型文件的最快方法+;? 在C++中创建一个50到100 MB的平面文本文件 对于内容,“添加的第一行”应使用旧式文件io插入到文件中400万次_C++_File Io_Iostream_Bulkinsert - Fatal编程技术网

用c+;创建大型文件的最快方法+;? 在C++中创建一个50到100 MB的平面文本文件 对于内容,“添加的第一行”应使用旧式文件io插入到文件中400万次

用c+;创建大型文件的最快方法+;? 在C++中创建一个50到100 MB的平面文本文件 对于内容,“添加的第一行”应使用旧式文件io插入到文件中400万次,c++,file-io,iostream,bulkinsert,C++,File Io,Iostream,Bulkinsert,fopen the file for write. fseek to the desired file size - 1. fwrite a single byte fclose the file create a string containing the "Added first line\n" a thousand times. find it's length. fopen the file for write. fseek to the the string length * 40

fopen the file for write.
fseek to the desired file size - 1.
fwrite a single byte
fclose the file

create a string containing the "Added first line\n" a thousand times.
find it's length.
fopen the file for write.
fseek to the the string length * 4000
fwrite a single byte
fclose the file

open the file for read/write
loop 4000 times, 
    writing the string to the file.
close the file.
fopen用于写入的文件

fseek到所需的文件大小-1

fwrite一个字节


fclose文件

创建特定大小文件的最快方法是使用
create()
open()
创建一个零长度文件,然后使用
chsize()
更改大小。这将简单地在磁盘上为文件分配块,内容将是这些块中发生的任何内容。速度非常快,因为不需要进行缓冲区写入。

我不确定我是否理解这个问题。是否要确保文件中的每个字符都是可打印的ASCII字符?如果是的话,这个呢?用“abcdefghabc….”填充文件

#包括
int main()
{
const int FILE_SiZE=50000;//大小(KB)
const int BUFFER_SIZE=1024;
字符缓冲区[缓冲区大小+1];
int i;
对于(i=0;i
您没有提到操作系统,但我假设create/open/close/write可用

对于真正高效的写入和假设(例如)4k页面和磁盘块大小以及重复的字符串:

  • 打开文件
  • 在重复的字符串中分配4k*个字符,最好与页面边界对齐
  • 将重复的字符串打印到内存中4k次,精确填充块
  • 使用write()将块写入磁盘的次数视需要而定。您可能希望为最后一块写一部分,以获得正确的大小
  • 关闭文件
  • 这绕过了fopen()和friends的缓冲,这是好的也是坏的:它们的缓冲意味着它们又好又快,但它们的效率仍然不如这一点,因为它们没有处理缓冲区的开销


    这很容易用C++或C编写,但是假设你将使用POSIX调用而不是IOFFROW或STDIO,因为它是在核心库规范之外的。<> P> < P> <强> >在C++中创建大文件的最快方法> < /> 好啊我认为最快的方式意味着运行时间最短的方式

    <强>在C++中创建一个50到100 MB的平面文本文件,其中“添加第一行”的内容应插入到文件中400万次。>/P> 使用旧式文件io预先分配文件

    fopen the file for write.
    fseek to the desired file size - 1.
    fwrite a single byte
    fclose the file
    
    create a string containing the "Added first line\n" a thousand times.
    find it's length.
    
    fopen the file for write.
    fseek to the the string length * 4000
    fwrite a single byte
    fclose the file
    
    open the file for read/write
    loop 4000 times, 
        writing the string to the file.
    close the file.
    
    使用旧式文件io预先分配文件

    fopen the file for write.
    fseek to the desired file size - 1.
    fwrite a single byte
    fclose the file
    
    create a string containing the "Added first line\n" a thousand times.
    find it's length.
    
    fopen the file for write.
    fseek to the the string length * 4000
    fwrite a single byte
    fclose the file
    
    open the file for read/write
    loop 4000 times, 
        writing the string to the file.
    close the file.
    
    这是我最好的猜测。
    我确信有很多方法可以做到这一点。

    我也面临同样的问题,在Windows上快速创建一个~500MB的文件。 传递给fwrite()的缓冲区越大,速度就越快

    int i;
    FILE *fp;
    
    fp = fopen(fname,"wb");
    
    if (fp != NULL) {
    
        // create big block's data
        uint8_t b[278528]; // some big chunk size
    
        for( i = 0; i < sizeof(b); i++ ) // custom initialization if != 0x00
        {
            b[i] = 0xFF;
        }
    
        // write all blocks to file
        for( i = 0; i < TOT_BLOCKS; i++ )
            fwrite(&b, sizeof(b), 1, fp);
    
        fclose (fp);
    }
    
    inti;
    文件*fp;
    fp=fopen(fname,“wb”);
    如果(fp!=NULL){
    //创建大区块的数据
    uint8_t b[278528];//一些大的块大小
    对于(i=0;i
    现在,至少在我的Win7上,MinGW几乎可以立即创建文件。 与每次写入1字节的fwrite()相比,这将在10秒内完成。
    通过4k缓冲区将在2秒内完成。

    我想这完全取决于您想要在文件中显示什么“文本”,以及您所说的“最佳”是什么意思。最快的?我想,反复编写一个包含一些示例文本的缓冲区将以最快的速度到达目的地。选择缓冲区的大小以获得最佳速度需要进行实验。“Lakh”是一个10000的印度单词。最初的问题(已被彻底修改)要求在文件中重复一段文本,并想知道这段文本的写入速度有多快。更改后的问题要求在顶部输入一个字符串。从下面的答案来看,它可能有其他的迭代。一个好的操作系统,应该把所有分配的块归零,作为一种安全措施。我注意到Novell Netware大约在1989年就这样做了,所以到现在为止,应该都这样做了。这取决于O/S。在大多数Unix系统上,如果您寻求偏移量,然后写入数据,最终将得到一个“稀疏文件”中间的块不占用磁盘上的任何空间。实际上没有分配块并用零填充块的事实既不存在也不存在。稀疏文件(至少在Unix上)将读取为零,这是您正在寻找的安全性。1)忘记了“添加的第一行”2)不会在所有OSs/文件系统上进行物理分配。有些保留了未分配的中间块。raj应该知道磁盘空间是否真的需要物理分配,还是只需要逻辑分配。4000万次意味着什么?哈哈。这解决了最初的问题。从那以后,它被编辑了一些。它需要一个新的答案。如果你想这样做,“man truncate”(在路径上工作)或ftruncate(带open()),它将扩展并截断文件到给定长度。我非常喜欢这个答案,但我是一个愚蠢的C新手,这感觉像是一个愚蠢的问题,但我们如何对齐页面边界?.4k页面从0开始均匀分布,所以你需要一个精确除以4k的指针。比如char*a=malloc(8192);char*aligned=(char*)((size_t)a+4096)和(size_t)4095)-如果您愿意,也可以使用4096-a%4096。你的目标是得到一个比a高,比a+4096低,正好除以4096的整数。