【发布时间】: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;
}
我想这样做。但问题是..我只能创建第一个文件,因为我只能从文件的开头读取数据。可以这样做吗?如果没有..拆分这些文件的最佳方法是什么。
【问题讨论】:
-
为什么只能从文件开头读取数据?
-
好吧,我不知道如何从文件的其他地方读取它