【问题标题】:Splitting files in C++在 C++ 中拆分文件
【发布时间】:2012-02-10 12:37:58
【问题描述】:

我需要将一个文件拆分为多个文件而不进行压缩。我在 cpp 参考资料中找到了这个

#include <fstream>
using namespace std;

int main () {

char * buffer;
long size;

ifstream infile ("test.txt",ifstream::binary);
ofstream outfile ("new.txt",ofstream::binary);

// get size of file
infile.seekg(0,ifstream::end);
size=infile.tellg();
infile.seekg(0);

// allocate memory for file content
buffer = new char [size];

// read content of infile
infile.read (buffer,size);

// write to outfile
outfile.write (buffer,size);

// release dynamically-allocated memory
delete[] buffer;

outfile.close();
infile.close();
return 0;
}

我想这样做。但问题是..我只能创建第一个文件,因为我只能从文件的开头读取数据。可以这样做吗?如果没有..拆分这些文件的最佳方法是什么。

【问题讨论】:

  • 为什么只能从文件开头读取数据?
  • 好吧,我不知道如何从文件的其他地方读取它

标签: c++ file fstream


【解决方案1】:

示例代码不会将一个文件拆分为多个文件;这只是 复制文件。要将文件拆分为多个文件,请不要关闭 输入。在伪代码中:

open input
decide size of each block
read first block
while block is not empty (read succeeded):
    open new output file
    write block
    close output file
    read another block

重要的部分是不要关闭输入文件,这样每次读取 准确地从前面的读取结束的地方开始。

【讨论】:

    【解决方案2】:

    您可以将流寻找到所需的位置,然后读取流。检查这段代码。

    // get size of file
    infile.seekg(0,ifstream::end);
    size=infile.tellg();
    infile.seekg(0);
    

    您需要做的就是记住您停止读取 infile 的位置、关闭 outfile、打开新的 outfile、重新分配缓冲区并将 infile 读取到缓冲区并写入第二个 outfile。

    【讨论】:

      【解决方案3】:

      您可以从文件中的任何位置读取数据 - 您已经成功移动到末尾并回到起点。

      您不需要这样做:只需编写一个循环来顺序读取每个outputSize 并将其写入一个新文件,对于某些outputSize &lt; size

      【讨论】:

        【解决方案4】:

        为什么要重新发明轮子 - 试试split

        如果你想用 C++ 实现它,甚至有源代码供你参考

        【讨论】:

          【解决方案5】:

          我想我已经找到了解决您问题的方法... 您读取了 char 数组中的所有第一个文件。 然后将数组的前半部分写入文件,然后将数组的后半部分写入其他文件...

          例如:

          #include <fstream>
          using namespace std;
          
          int main () {
          
          char * buffer;
          long size;
          
          ifstream infile ("test.txt",ifstream::binary);
          ofstream outfile ("new.txt",ofstream::binary);
          ofstream outfile2 ("new2.txt",ofstream::binary);
          
          // get size of file
          infile.seekg(0,ifstream::end);
          size=infile.tellg();
          infile.seekg(0);
          
          // allocate memory for file content
          buffer = new char [size];
          
          // read content of infile
          infile.read (buffer,size);
          
          // write to outfile
          outfile.write (buffer,size/2);
          outfile2.write (buffer+size/2,size);
          
          // release dynamically-allocated memory
          delete[] buffer;
          
          outfile.close();
          infile.close();
          outfile2.close();
          return 0;
          }
          

          你也可以读前半部分,写,然后读后半部分,再写……看看吧:

          int main () {
          
          char * buffer;
          long size;
          long halfSize;
          ifstream infile ("test.txt",ifstream::binary);
          ofstream outfile ("new.txt",ofstream::binary);
          ofstream outfile2 ("new2.txt",ofstream::binary);
          
          // get size of file
          infile.seekg(0,ifstream::end);
          size=infile.tellg();
          infile.seekg(0);
          halfSize = static_cast<int>(floor(size/2));
          // allocate memory for file content
          
          buffer1 = new char[halfSize];
          buffer2 = new char[size-halfSize];
          
          // read content of infile
          infile.read (buffer1,halfSize);
          infile.read (buffer2,size-halfSize);
          
          // write to outfile
          outfile.write (buffer1,halfSize);
          outfile2.write (buffer2,size-halfSize);
          
          // release dynamically-allocated memory
          delete[] buffer;
          delete[] buffer2;
          
          outfile.close();
          infile.close();
          outfile2.close();
          return 0;
          }
          

          【讨论】:

            猜你喜欢
            • 2011-02-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-10-22
            • 2011-04-04
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多