工作以后基本不用C,全是C++,今天复习C语言中的文件操作,顺便写了个简单版本的文件分割和合并的程序。
// FileSplitter.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <boost/lexical_cast.hpp>
#include <string>
#include <assert.h>
#include <boost/foreach.hpp>
#include <vector>
namespace
{
std::string DecoratePath(const std::string& dirPath)
{
char slash = '//';
char slash_ex = '/';
std::string path = dirPath;
int nPathLen = static_cast<int>(path.length());
if (nPathLen != 0 && (dirPath[nPathLen - 1] != slash || dirPath[nPathLen - 1] != slash_ex))
path += slash;
return path;
}
bool SplitFile(const char* fileName, int nBlockSize, const char* destPath)
{
assert(nBlockSize > 0);
if (nBlockSize <= 0)
return false;
FILE *pFile = fopen(fileName, "rb");
if (pFile == NULL)
return false;
//how big
fseek(pFile, 0, SEEK_SET);
fseek(pFile, 0, SEEK_END);
long nFileLen = ftell(pFile);
//seeking back to the beginning
fseek(pFile, 0, SEEK_SET);
char *buf= new char[nBlockSize];
int nCnts = 0;
//not reach the end of file
while (feof(pFile) == 0)
{
std::string fileName = DecoratePath(destPath);
fileName = fileName + "part" + boost::lexical_cast<std::string>(++nCnts);
FILE *pPartFile = fopen(fileName.c_str(), "wb");
if (!pPartFile)
continue;
int nRead = fread(buf, sizeof(char), nBlockSize, pFile);
fwrite(buf, sizeof(char), nRead, pPartFile);
fclose(pPartFile);
}
delete []buf;
fclose(pFile);
return true;
}
typedef std::vector<std::string> Files_t;
bool CombineFiles(const Files_t& files, const std::string& destFile)
{
bool fEmpty = files.empty();
assert(!fEmpty);
if (fEmpty)
return false;
FILE *pFile = fopen(destFile.c_str(), "wb");
if (!pFile)
return false;
{
const int nBuffer = 1024 * 1024 * 2; // 2M
char *pBuf = new char[nBuffer];
BOOST_FOREACH(const std::string& file, files)
{
FILE *pPartFile = fopen(file.c_str(), "rb");
if (pPartFile == NULL)
continue;
while (feof(pPartFile) == 0)
{
int nRead = fread(pBuf, sizeof(char), nBuffer, pPartFile);
fwrite(pBuf, sizeof(char), nRead, pFile);
}
fclose(pPartFile);
}
delete []pBuf;
}
fclose(pFile);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
//该文件是2k的,1k一个文件
SplitFile("g://2.txt", 1024 * 1, "g://");
Files_t files;
files.push_back("g://part1");
files.push_back("g://part2");
CombineFiles(files, "g://new2.txt");
return 0;
}
// FileSplitter.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <boost/lexical_cast.hpp>
#include <string>
#include <assert.h>
#include <boost/foreach.hpp>
#include <vector>
namespace
{
std::string DecoratePath(const std::string& dirPath)
{
char slash = '//';
char slash_ex = '/';
std::string path = dirPath;
int nPathLen = static_cast<int>(path.length());
if (nPathLen != 0 && (dirPath[nPathLen - 1] != slash || dirPath[nPathLen - 1] != slash_ex))
path += slash;
return path;
}
bool SplitFile(const char* fileName, int nBlockSize, const char* destPath)
{
assert(nBlockSize > 0);
if (nBlockSize <= 0)
return false;
FILE *pFile = fopen(fileName, "rb");
if (pFile == NULL)
return false;
//how big
fseek(pFile, 0, SEEK_SET);
fseek(pFile, 0, SEEK_END);
long nFileLen = ftell(pFile);
//seeking back to the beginning
fseek(pFile, 0, SEEK_SET);
char *buf= new char[nBlockSize];
int nCnts = 0;
//not reach the end of file
while (feof(pFile) == 0)
{
std::string fileName = DecoratePath(destPath);
fileName = fileName + "part" + boost::lexical_cast<std::string>(++nCnts);
FILE *pPartFile = fopen(fileName.c_str(), "wb");
if (!pPartFile)
continue;
int nRead = fread(buf, sizeof(char), nBlockSize, pFile);
fwrite(buf, sizeof(char), nRead, pPartFile);
fclose(pPartFile);
}
delete []buf;
fclose(pFile);
return true;
}
typedef std::vector<std::string> Files_t;
bool CombineFiles(const Files_t& files, const std::string& destFile)
{
bool fEmpty = files.empty();
assert(!fEmpty);
if (fEmpty)
return false;
FILE *pFile = fopen(destFile.c_str(), "wb");
if (!pFile)
return false;
{
const int nBuffer = 1024 * 1024 * 2; // 2M
char *pBuf = new char[nBuffer];
BOOST_FOREACH(const std::string& file, files)
{
FILE *pPartFile = fopen(file.c_str(), "rb");
if (pPartFile == NULL)
continue;
while (feof(pPartFile) == 0)
{
int nRead = fread(pBuf, sizeof(char), nBuffer, pPartFile);
fwrite(pBuf, sizeof(char), nRead, pFile);
}
fclose(pPartFile);
}
delete []pBuf;
}
fclose(pFile);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
//该文件是2k的,1k一个文件
SplitFile("g://2.txt", 1024 * 1, "g://");
Files_t files;
files.push_back("g://part1");
files.push_back("g://part2");
CombineFiles(files, "g://new2.txt");
return 0;
}