Chinaunix首页 | 论坛 | 博客
  • 博客访问: 291290
  • 博文数量: 111
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 816
  • 用 户 组: 普通用户
  • 注册时间: 2014-05-04 20:35
文章分类

全部博文(111)

文章存档

2016年(1)

2015年(5)

2014年(105)

我的朋友

分类: C/C++

2014-08-13 20:14:48


点击(此处)折叠或打开

  1. #include <iostream>
  2. #include <fstream>
  3. #include "stdlib.h"

  4. #include <string>
  5. #include <windows.h>
  6. #include <sstream>


  7. using namespace std;

  8. namespace Tool
  9. {
  10.     class FileHelper
  11.     {
  12.         public:
  13.             FileHelper();
  14.             ~FileHelper();
  15.             void CopyFile1();
  16.             void CopyFile2();
  17.             void CopyFile3();
  18.             void CreatFile1();
  19.             void WriteFile();

  20.         private:

  21.     };

  22.         FileHelper::FileHelper()
  23.         {
  24.             ifstream examplefile ("example.txt");
  25.              std::ifstream input("E:\\Software\\SW_DVD5_Project_Pro_2013_64Bit_ChnSimp_MLF_X18-55123.ISO",ios::binary);
  26.              std::ofstream output("E:\\Software\\SW_DVD5_Project_Pro_2013_64Bit_ChnSimp_MLF_X18-55123_1.ISO",ios::binary);
  27.              char ch;
  28.              while (input.get(ch)) output << ch;

  29.         }

  30.         FileHelper::~FileHelper()
  31.         {}

  32.         //效率较高
  33.         void FileHelper::CopyFile1()
  34.         {
  35.             //c style writing file
  36.             FILE* fp = fopen("E:\\Software\\SW_DVD5_Project_Pro_2013_64Bit_ChnSimp_MLF_X18-55123.ISO","rb");
  37.             FILE* fp1 = fopen("E:\\Software\\SW_DVD5_Project_Pro_2013_64Bit_ChnSimp_MLF_X18-55123_1.ISO", "ab");
  38.             const int BUF_SIZE = 1024*100;
  39.             char buf[BUF_SIZE];
  40.             int len = 0;
  41.             do
  42.             {
  43.                 len = fread(buf,1,BUF_SIZE,fp);
  44.                 fwrite(buf, 1, len, fp1);
  45.             }while(len != 0);
  46.             //15s

  47.             fclose(fp);
  48.             fclose(fp1);
  49.         }
  50.         void FileHelper::CopyFile2()
  51.         {
  52.             std::ifstream input("E:\\Software\\SW_DVD5_Project_Pro_2013_64Bit_ChnSimp_MLF_X18-55123.ISO",ios::binary);
  53.             std::ofstream output("E:\\Software\\SW_DVD5_Project_Pro_2013_64Bit_ChnSimp_MLF_X18-55123_1.ISO",ios::binary|ios::app);

  54.             const int max = 1024*512;
  55.             char ch[max];
  56.             while (!input.eof())
  57.             {
  58.                 input.read(ch, max);
  59.                 output.write(ch, input.gcount());
  60.             }
  61.     
  62.             output.flush();
  63.         }
  64.         void FileHelper::CopyFile3()
  65.         {
  66.             string sourceFile = "E:\\Software\\SW_DVD5_Project_Pro_2013_64Bit_ChnSimp_MLF_X18-55123.ISO";
  67.             string destFile = "E:\\Software\\SW_DVD5_Project_Pro_2013_64Bit_ChnSimp_MLF_X18-55123_1.ISO";
  68.             CopyFile(sourceFile.c_str(), destFile.c_str(), TRUE);
  69.         }

  70.         void FileHelper::CreatFile1()
  71.         {
  72.             string testFolder="E:\\temp\\Copy2";
  73.             for (int i = 0; i < 1000; i++)
  74.             {
  75.                 stringstream sstrm;
  76.                 sstrm<<i;
  77.                 string filename = testFolder+"\\"+sstrm.str()+".txt";
  78.                 CreateFile(filename.c_str(),GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
  79.             }
  80.             //0.2s
  81.         }

  82.         void FileHelper::WriteFile()
  83.         {
  84.             //file
  85.             ofstream examplefile ("name.txt",ios::app);

  86.             if (examplefile.is_open()) {

  87.             examplefile << "\n";
  88.             examplefile << "This is a line.\n";
  89.             examplefile << "This is another line.\n";

  90.             examplefile.close();
  91.             }
  92.         }
  93. }

阅读(706) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~