文件分割器V1.0

终于,做出来了,
虽然程序很简单,但是使我发现了一个人人都应该值得注意的事情,
就是即使简单,要做好,还是有难度的.
一下为文档,以作纪念.

文件拆分器V1.0

具有功能:

       把比较长的文档或文件拆分成若干份小文件.目前只能把文件分割成txt文件.而且目前的功能只能最多分割出二十个文件.

 

 

 

控件变量:

 

              CString m_ReAddress;            //原文件地址

              Cstring m_ToAddress;                      //存放到地址

              long m_Size;                                   //文件字节数

              long m_PerSize;                               //每份大小;

              Cstring m_status                              //显示分割后的状态

             

 

 

代码:

       //class CmyDlg中加入几个成员:

       Class CmyDlg

       {

                     long size;                      //记录导入文档的长度;

                     int per;                         //把文档分成的分数

                     char *name[20];           //分割后文件的名称

                     CString filename;         //分割后文件的地址及名称

}

 

 

//构造函数

CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)

       : CDialog(CMyDlg::IDD, pParent)

{

       name[0]="000.txt";

       name[1]="001.txt";

       name[2]="002.txt";

       name[3]="003.txt";

       name[4]="004.txt";

       name[5]="005.txt";

       name[6]="006.txt";

       name[7]="007.txt";

       name[8]="008.txt";

       name[9]="009.txt";

       name[10]="010.txt";

       name[11]="011.txt";

       name[12]="012.txt";

       name[13]="013.txt";

       name[14]="014.txt";

       name[15]="015.txt";

       name[16]="016.txt";

       name[17]="017.txt";

       name[18]="018.txt";

       name[19]="019.txt";

       per=0;

       size=0;

}

 

 

导入部分:

void CMyDlg::OnImport()

{

       // TODO: Add your control notification handler code here

       UpdateData(true);

       char *readdress=m_Readdress.GetBuffer(m_Readdress.GetLength());

       fstream infile(readdress,ios::in | ios::nocreate | ios::binary);

       if(!infile)

       {

              MessageBox("can't not fine this file,please input the right filename.","error");

       }

       else

       {

              infile.seekg(0,ios::end);

              size=infile.tellg();

              m_Size=size;

              infile.seekg(0,ios::beg);

       }

       infile.close();

       UpdateData(false);

}

 

 

保存部分:

 

void CMyDlg::OnSave()

{

       // TODO: Add your control notification handler code here

       UpdateData(true);

       if(m_PerSize<=0&&m_PerSize>=size)

              MessageBox("PerSize must bigger than zero and smaller than fileSize","error");

       else

       {

              per=size/m_PerSize;

              if((m_PerSize*per)<size) per++;

              char *readdress=m_Readdress.GetBuffer(m_Readdress.GetLength());

              fstream infile(readdress,ios::in | ios::nocreate | ios::binary );              

              m_Readdress.ReleaseBuffer();

              for(int n=0;n<per;n++)

              {

                     char c;

                     filename=m_Toaddress+name[n];

                     char *FileName=filename.GetBuffer(filename.GetLength());

                     filename.ReleaseBuffer();

                     fstream outfile(FileName,ios::out);                                                        

                     for(int i=0;i<m_PerSize;i++)

                     {

                            infile.get(c);

                            outfile.put(c);

                     }

                     outfile.close();

              }

              infile.close();

              MessageBox("已经把文件成功分割了.","yeah");

              CString perfile;

              perfile.Format("%d",per);

              m_status="已经把文件分割成 "+perfile+" .";

       }

       UpdateData(false);

}

 

//--------------------下面为错误的代码(read,write的方法读入,)本来想提高效率,可是,出现了错误,没办法不用续字复制的方法;//

void CMyDlg::OnSave()

{

       // TODO: Add your control notification handler code here

       UpdateData(true);

       if(m_PerSize<=0&&m_PerSize>=size)

              MessageBox("PerSize must bigger than zero and smaller than fileSize","error");

       else

       {

              per=size/m_PerSize;

              char *readdress=m_Readdress.GetBuffer(m_Readdress.GetLength());

              fstream infile(readdress,ios::in | ios::nocreate | ios::binary);

              for(int n=0;n<per;n++)

              {

                     char *temp=0;

                     fstream outfile(name[n],ios::out | ios::binary);

                     infile.read(temp,m_PerSize);

                     outfile.write(temp,m_PerSize);

                     outfile.close();

              }

              infile.close();

              MessageBox("job complish.","yeah");

       }

       UpdateData(false);

}

 

 

你可能感兴趣的:(ios,File,null,Class,文档,input)