zoukankan      html  css  js  c++  java
  • 合并两个文件,替换文件,获取文件大小

    合并文件   
    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;
    }
  • 相关阅读:
    Rotation issue in XCode 4.5 and iOS 6.0
    Core Data on iOS 5 Tutorial: How To Work with Relations and Predicates
    How To Synchronize Core Data with a Web Service – Part 2
    Unit Testing in Xcode 4 – use OCUnit and SenTest instead of GHUnit
    Migrate old project to ARC
    Core Data on iOS 5 Tutorial: Getting Started
    How To Draw Graphs with Core Plot, Part 2
    How To Use NSOperations and NSOperationQueues
    How To Save Your App Data With NSCoding and NSFileManager
    What's New in iOS
  • 原文地址:https://www.cnblogs.com/lizhanzhe/p/10878151.html
Copyright © 2011-2022 走看看