合并两个文件,替换文件,获取文件大小

合并文件   
void OpMergerFile(string readpath, string writepath)
{
//    char cline;
//    ifstream ifile;
//    ofstream ofile;
//    if (_access(readpath.c_str(), 0) == 0) {
//#ifdef WIN32
//        ifile.open(readpath, ios::binary | ios::in);     //读写都要以二进制形式,否则遇到/0就认为结束
//        ofile.open(writepath,  ios::binary | ios::app);
//#else
//        ifile.open(readpath, ios::binary);
//        ofile.open(writepath, fstream::app);
//#endif // WIN32
//        while (ifile.get(cline))
//            ofile << cline;
//
//        ifile.close();
//        ofile.close();
    //}

   char c;
   FILE* rfp, *wfp;
   fopen_s(&rfp, readpath.c_str(), "rb");
  fopen_s(&wfp, writepath.c_str(), "a+b");
 
  
  fseek(wfp, 0, SEEK_END); 

  while (fread(&c, 1, 1, rfp))
  fwrite(&c, 1, 1, wfp);
  fclose(rfp); fclose(wfp);

return; }


替换文件

void OpReplaceFile(string readpath,string writepath)
{
    char cline;
    ifstream ifile;
    ofstream ofile;
    if (IfFileExist(readpath.c_str()) && IfFileExist(writepath.c_str())) {
#ifdef WIN32
    ifile.open(readpath, ios::binary);
    ofile.open(writepath, ios::binary);
#else
    ifile.open(readpath);
    ofile.open(writepath);
#endif // WIN32
        
        while(ifile.get(cline))
            ofile << cline;
        ifile.close();
        ofile.close();
    }
    return;
}

拼接文件(保留其中一个文件不变)

void ToRightMergerFile(string MergerFilepath,string HoleFilePath)
{
    std::string tmp = "./intertemp";
    MergerFile(HoleFilePath, tmp);
    MergerFile(MergerFilepath,tmp);
    OpReplaceFile(tmp, MergerFilepath);
    remove(tmp.c_str());
}

 

 获取文件大小

long GetFileLength(const std::string filepath)
{
    long LocalFilelen = 0;
    FILE* pfile;
#ifdef WIN32
    if (fopen_s(&pfile, filepath.c_str(), "r") != 0)
        return 0;
    if(pfile)
    {
        fseek(pfile, 0, SEEK_SET);
        fseek(pfile, 0, SEEK_END);
        LocalFilelen = ftell(pfile);
    }
#else
    pfile = fopen(filepath.c_str(), "r");
    if(pfile)
    {
        fseek(pfile, 0, SEEK_SET);
        fseek(pfile, 0, SEEK_END);
        LocalFilelen = ftell(pfile);
    }
#endif

    fclose(pfile);
    pfile = NULL;
    return LocalFilelen;
}

 

转载于:https://www.cnblogs.com/lizhanzhe/p/10878151.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值