📄 文件操作dlg.cpp
字号:
//int iBytesRead; //临时变量
char *pszBuffer;//临时变量
long buf1;//文件缓冲区变量2
int buf;
int num;
if(choose==1)
buf=OnEditchangeCombo1() ;
if(choose==2)
buf=OnChangeEdit3() ;
CString list,list1;//为最后的输出作准备
CFile File;
CString b;
char c=92;
b.Format("%c",c);//得到"\"
if(ss==1)
{//MessageBox(m_pEdit2);
if((m_edit1 =="")||(m_edit1 =="*.*"))
{
MessageBox("请选择要分割的文件");
return ;
}
if(m_edit2==""||m_edit2=="\\")
{
MessageBox("请选择分割后的文件存储路径");
return;
}
if(buf<=0.0)
{
MessageBox("请选择要分割成文件的大小");
return;
}
// TODO: Add your control notification handler code here
buf1=buf*1024L;//把缓冲区转化为字节
File.Open(filename1,CFile::modeRead);//打开文件句柄
FileLength=File.Seek(0L,CFile::end);//测试文件长度
num=FileLength/buf1+1;//计算分割的文件个数
m_progress.SetRange(0,num);//////////////////////////////////
File.Close();//关闭文件
if (FileLength<=buf1)
{
MessageBox("文件小于要分割的大小,不用分割!");
return;}
int i;
pszBuffer=new char [buf1];
int temp=buf1;
OnChangeEdit4(num);
OnChangeEdit5(FileLength);
for (i=1;i<=num;i++)
{
if (i==num)
{pszBuffer=new char[FileLength-buf1*(num-1)];
temp=FileLength-buf1*(num-1);
}
File.Open(m_edit1 ,CFile::modeRead|CFile::shareExclusive|CFile::typeBinary);//打开文件句柄
File.Seek(buf1*(i-1),CFile::begin);//定位读文件位置
File.Read(pszBuffer,temp);
File.Close();//关闭源文件
CString str_num;
str_num.Format("%d",i);
CString Path;
CString Cut=".Cut";
Path=m_edit2+b+filename1+Cut+str_num;
CString str_unm1;
str_unm1.Format("%d",temp/1024);
CString c=" ";
list=Path+c+str_unm1+"KB"+c;
list1+=list+"\r\n";
File.Open(Path,CFile::modeWrite|CFile::shareExclusive|CFile::typeBinary|CFile::modeCreate);//|CFile::modeNoInherit);
File.Write(pszBuffer,temp);
m_progress.SetPos(i);////////////////////////////////////
File.Close();//关闭目标文件
}
MessageBox("恭喜文件分割完成!");
}
else if(ss==2)
{
int joinnum;//计算文件合成的个数
CString Path1,Path2;
if((m_edit1 =="")||(m_edit1 =="*.*"))
{MessageBox("请选择要合成的文件");
return ;
}
if(m_edit2==""||m_edit2=="\\")
{MessageBox("请选择合成后的文件存储路径");
return;
}
Path1=m_edit1 ;
Path1.Delete(Path1.GetLength()-1,1);//在此得到合并文件路径去掉了最后编号
CString Path4;
Path2=filename1;
Path2.Delete(filename1.GetLength()-1,1);//在此得到合并文件路径去掉了最后编号
Path4=m_edit2+b+Path2;
Path4.Delete(Path4.GetLength()-4,4);
for(joinnum=1;;joinnum++)
{ CString str_num1;
str_num1.Format("%d",joinnum);
CString Path3;
Path3=Path1+str_num1;
if(File.Open(Path3,CFile::modeRead|CFile::shareExclusive|CFile::typeBinary)==NULL)//打开要合并文件句柄
break;
File.Close();
}
joinnum--;
num=joinnum;
m_progress.SetRange(0,num);
int j;
long int d=0;
long int FileLength3=0;//统计所有文件大小
for(j=1;j<=joinnum;j++)
{
CString str_num2;
str_num2.Format("%d",j);
long int FileLength2;//每个文件大小
CString Path3;
Path3=Path1+str_num2;
File.Open(Path3,CFile::modeRead|CFile::shareExclusive|CFile::typeBinary);
FileLength2=File.Seek(0L,CFile::end);//查找文件长度,开始写文件
d+=FileLength2;
CString str_unm1;
str_unm1.Format("%d",FileLength2/1024);
CString c=" ";
list=Path3+c+str_unm1+"KB"+c;
list1+=list+"\r\n";
OnChangeEdit4(joinnum);
File.SeekToBegin();
pszBuffer=new char [FileLength2];
File.Read(pszBuffer,FileLength2);
File.Close();
File.Open(Path4,CFile::modeWrite|CFile::shareExclusive|CFile::typeBinary|CFile::modeCreate|CFile::modeNoTruncate);
File.SeekToEnd();//查找文件长度
File.Write(pszBuffer,FileLength2);
File.Close();
}
OnChangeEdit5(d);
MessageBox("恭喜文件合成完成!");
}
OnChangeEdit6(list1);
if(ss==3)
{CString b;
char c=92;
b.Format("%c",c);//得到"\"
char *data;
CFile *file;
DWORD flen;
CString ppass1;
__int64 m_password;
OnChangeEdit8();////////////////////////////////////////////////////////////
if(m_pass=="")
{MessageBox("请输入加密文件的密码", "",MB_OK | MB_ICONEXCLAMATION);
return ;
}
if(m_pass1=="")
{MessageBox("请确认加密文件的密码", "",MB_OK | MB_ICONEXCLAMATION);
return ;
}
if(m_pass!=passwordcheck)
{ MessageBox("密码确认错误重新确认或者退出", "",MB_OK | MB_ICONEXCLAMATION);
//m_Radio4.SetCheck(0);
//m_Radio5.SetCheck(1);
return ;
}
m_password=epass();//密码初始化
file = new CFile;
if ( !file->Open(m_edit1, CFile::shareDenyNone|CFile::modeReadWrite))
{
return ;
}
CString patch;
CFile File;
patch=m_edit2+b+filename1;
flen=file->GetLength();
data = new char[(int)flen];//为文件分配内存
file->SeekToBegin();
file->Read(data,flen);
file->Close();
file->Open(m_edit2+b+filename1, CFile::shareDenyNone|CFile::modeReadWrite|CFile::modeCreate);
if(!file)
{MessageBox("文件创建错误,操作退出!", "",MB_OK | MB_ICONEXCLAMATION);
return;
}
//这里把文件的所有字节都进行加密了
for(DWORD i=0; i<flen; i++)
{
data[i] ^= m_password;
data[i] ^= flen; //因为每次加密后文件的大小都会增加5字节,所以如果两次用同一密码进行加密也没关系
}
file->SeekToBegin();
file->Write(data, flen);
delete[] data; //先释放内存
//添加密码验证信息
char cpass[15];
for(int j=0;j<15;j++)
{cpass[j]=1;
}
char *a=NULL;
int length;
length=m_pass.GetLength();
a=m_pass.GetBuffer(length);
for(j=0;j<length;j++,a++)
{cpass[j]=*a;
}
file->SeekToEnd();
file->Write(&cpass,15); //在文件尾添加密码严整部分,,这样文件就会增加5字节
file->Close();
delete file;
MessageBox("恭喜加密已经成功");
return ;
}
if(ss==4)
{char *data;
CFile *file;
DWORD flen;
char love[15];
__int64 m_password;
file = new CFile;
if( !file->Open(m_edit1, CFile::shareDenyNone|CFile::modeReadWrite))
{
return ;
}
flen = file->GetLength();
data = new char[(int)flen];
//检验密码是不是正确
file->Seek(-15, CFile::end);
file->Read(&love, 15);
m_password=epass();
char cpass[15];
for(int j=0;j<15;j++)
{cpass[j]=1;
}
char *a=NULL;
int length;
length=m_pass.GetLength();
a=m_pass.GetBuffer(length);
for( j=0;j<length;j++,a++)
{cpass[j]=*a;
}
for(j=0;j<15;j++)
{if(cpass[j]!=love[j])
{
MessageBox("解密密码错误,重新输入密码或者退出!", "",MB_OK | MB_ICONEXCLAMATION);
return ;
}
}
//解密
CString patch;
CFile File;
patch=m_edit2+b+filename1;
file->SeekToBegin();
file->Read(data, flen);
for(j=0; j<(int)flen; j++)
{
data[j] += m_password;
data[j] += (flen-15);
// m_pProgress.SetPos(j/1024);
}
;
file->Close();
file->Open(m_edit2+b+filename1, CFile::shareDenyNone|CFile::modeReadWrite|CFile::modeCreate);
if(!file)
{MessageBox("文件创建错误,操作退出!");
return;
}
file->SeekToBegin();
file->Write(data, flen);
file->SetLength(flen-15); //删除加密是添加的密码验证部分
file->Close();
delete[] data;
delete file;
MessageBox("恭喜文件解密完成!");
return ;
}
}
void CMyDlg::OnCancel()
{
// TODO: Add extra cleanup here
CDialog::OnCancel();
}
void CMyDlg::OnButton4()
{
// TODO: Add your control notification handler code here
CAboutDlg j;
j.DoModal();
}
void CMyDlg::OnRadio6()
{
// TODO: Add your control notification handler code here
ss=3;
}
void CMyDlg::OnRadio7()
{
// TODO: Add your control notification handler code here
ss=4;
}
__int64 CMyDlg::epass()
{
DWORD plen;
char *ppass;
__int64 mc= 8757735233301;
UpdateData(TRUE);
ppass = m_pass.GetBuffer(0);
plen = strlen(ppass);
for(int i=0; i<(int)plen; i++)
{
mc -= ppass[i]|128;
}
return mc;
}
void CMyDlg::OnChangeEdit8()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
UpdateData(TRUE);
int length;
length=m_pass1.GetLength();
passwordcheck=m_pass1.GetBuffer(length);
}
void CMyDlg::OnChangeEdit7()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -