合并没有空间的文件比合并有空间的文件快
Merging File without space is faster than with space
因此,我创建了一个用于合并文件的c++可执行文件。我有43个文件,每个文件大小为100MB。所以总共有4.3GB。
两种情况:
1:如果文件名为1,2,3,4,5,6,…, 43合并完成大约需要2分钟。
二:如果文件名是This file。ova0,这个文件。ova1,……,此文件。Ova42完成合并大约需要7分钟。
这是完全相同的文件,我只是重命名了文件。知道怎么了吗?
这是c++代码
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include "boost/filesystem.hpp"
namespace bfs = boost::filesystem;
#pragma warning(disable : 4244)
typedef std::vector<std::string> FileVector;
int main(int argc, char **argv)
{
int bucketSize = 3024 * 3024;
FileVector Files;
//Check all command-line params to see if they exist..
for(int i = 1; i < argc; i++)
{
if(!bfs::exists(argv[i]))
{
std::cerr << "Failed to locate required part file: " << argv[i] << std::endl;
return 1;
}
//Store this file and continue on..
std::cout << "ADDING " << argv[i] << std::endl;
Files.push_back(argv[i]);
}
//Prepare to combine all the files..
FILE *FinalFile = fopen("abc def.ova", "ab");
for(int i = 0; i < Files.size(); i++)
{
FILE *ThisFile = fopen(Files[i].c_str(), "rb");
char *dataBucket = new char[bucketSize];
std::cout << "Combining " << Files[i].c_str() << "..." << std::endl;
//Read the file in chucks so we do not chew up all the memory..
while(long read_size = (fread(dataBucket, 1, bucketSize, ThisFile)))
{
//FILE *FinalFile = fopen("abc def.ova", "ab");
//::fseek(FinalFile, 0, SEEK_END);
fwrite(dataBucket, 1, read_size, FinalFile);
//fclose(FinalFile);
}
delete [] dataBucket;
fclose(ThisFile);
}
fclose(FinalFile);
return 0;
}
我像这样通过.bat文件运行它:
@ECHO OFF
Combiner.exe "This File.ova0" "This File.ova1" "This File.ova2"
PAUSE
或
@ECHO OFF
Combiner.exe 1 2 3
PAUSE
两个。bat文件放到文件名的末尾,我在这里只写了3个文件,否则就太长了
谢谢
默认情况下,Windows缓存从磁盘读取并写入磁盘的文件数据。这意味着读取操作从系统内存中称为系统文件缓存的区域读取文件数据,而不是从物理磁盘读取文件数据。相应地,写操作将文件数据写入系统文件缓存,而不是写入磁盘,这种类型的缓存称为回写缓存。缓存是按文件对象管理的:更多信息:文件缓存
相关文章:
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 命名空间中具有.h和.cpp文件的类
- 使用 Clang++ 有没有办法将文件作为命名空间等包含?
- Qt C++:不同文件中同一命名空间的多个Q_NAMESPACE
- 不同C++文件中未命名命名空间中的名称可以引用同一个命名事物吗?
- 使用命名空间在另一个".cpp"文件中定义
- 在命名空间中声明变量,在 main 中定义它,使其对所有其他文件可见
- 文件删除(使用取消链接)与释放所有磁盘空间之间的延迟
- 是否将命名空间范围文件本地 (.cpp) 常量放在匿名命名空间中
- 模板实现文件中的匿名命名空间
- C++-使用来自不同头文件、相同命名空间的枚举
- C++ 如何在 def 文件中指定命名空间
- 在C++中读取文件时增加了空间
- 在实现文件中,我们应该更喜欢"using namespace"指令还是将实现包装在命名空间 { } 中?
- 在头文件中添加命名空间函数
- 在多个 cpp 文件中重用未命名的命名空间函数
- 使用匿名命名空间中的函数或另一个文件中的静态函数不应该出错吗?
- 跨命名空间的友元类和不同的.H 文件
- C 提升ifstream读取文件内容,带有白色空间,折断,标签等
- TFS 2013构建如何使工作空间文件可编辑