2012-02-10 144 views
1

我需要将文件拆分为多个文件而不进行压缩。我发现这在CPP参考将文件拆分为C++

#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; 
} 

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

+0

为什么只能从文件的开头读取数据? – 2012-02-10 12:40:40

+0

那么,我不知道如何从文件的其他地方读取它 – Transcendental 2012-02-10 12:45:59

回答

0

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

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

所有你需要做的是要记住的位置,你不读INFILE,接近OUTFILE,开辟新的OUTFILE,重新分配缓冲区和阅读INFILE缓冲和写入第二OUTFILE。

0

您可以从文件的任何位置读取数据 - 您已经移动到最后并成功启动。

虽然您不需要:只需编写一个循环来顺序读取每个outputSize并将其写入新文件,对于某些outputSize < size

0

为什么推倒重来 - 尝试split

甚至源代码给你,如果你想实现它在C++

0

我想我已经得到了解决您的问题得到的想法...... 您读取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.seekg(halfSize+1); 
// read content of infile 
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; 
} 
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 

的重要部分不关闭输入文件,以使每个读 拾取确切位置前述读取结束。