VC 写 TXT 文件分割器 附代码

TXT文件分割器主要将很大的TXT文档分割成预定大小的文档

主要通过cfile类来实现

这就是最后的效果。

选择路径主要通过cfiledlg类来实现

	// TODO: Add your control notification handler code here
	CFileDialog file_dlg(true , NULL , NULL , OFN_OVERWRITEPROMPT | OFN_ALLOWMULTISELECT ,NULL , NULL);
	if (file_dlg.DoModal() == IDOK)
	{
		filepath = file_dlg.GetPathName() ;		
	}
	m_path = filepath ;
	UpdateData(false);


 // TODO: Add your control notification handler code here
 CFileDialog file_dlg(true , NULL , NULL , OFN_OVERWRITEPROMPT | OFN_ALLOWMULTISELECT ,NULL , NULL);
 if (file_dlg.DoModal() == IDOK)
 {
  filepath = file_dlg.GetPathName() ;  
 }
 m_path = filepath ;
 UpdateData(false); // TODO: Add your control notification handler code here
 CFileDialog file_dlg(true , NULL , NULL , OFN_OVERWRITEPROMPT | OFN_ALLOWMULTISELECT ,NULL , NULL);
 if (file_dlg.DoModal() == IDOK)
 {
  filepath = file_dlg.GetPathName() ;  
 }
 m_path = filepath ;
 UpdateData(false);m_path 是源文件的控件变量

 

保存目录这部分代码是借鉴网上的

	BROWSEINFO bi;
	bi.hwndOwner = this->GetSafeHwnd();
	bi.pidlRoot = NULL;
	bi.pszDisplayName = NULL;
	bi.lpszTitle = TEXT("请选择文件夹");
	bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT ;
	bi.lpfn = NULL;
	bi.lParam = 0 ;
	bi.iImage = 0 ;
	
	LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
	if (pidl == NULL)
	{
		return ;
	}
	char s[100];
	if ( SHGetPathFromIDList(pidl , s) )
	{
		m_path1 = s ;
	}
	UpdateData(false);


m_path1是生成文件夹的控件变量

 

 

开始分割按钮主要实现下面代码:

	index = m_com.GetCurSel() + 1 ;     

	CFile infile;
	file.Open(filepath , CFile::modeCreate | CFile::modeNoTruncate | CFile::modeReadWrite );    
	
	CString str,ss,s;
	str.Format("文件大小为%.2fkb",file.GetLength() / 1024.0 );
	GetDlgItem(IDC_STATIC1)->SetWindowText(str);
	
	int n;
	char buff[1024];       



	ss = m_path1 + "\\";
	ss += file.GetFileName() ;
	ss = ss.Left(ss.GetLength() - 4) ;
	ss += "_";	

	for (int i = 0 ; i <= file.GetLength() / index /1024 ; ++i )
	{


		str = ss ;
		s.Format("%d",i);
		str += s;
		str += ".txt";
		infile.Open(str ,  CFile::modeCreate | CFile::modeNoTruncate | CFile::modeReadWrite );     
		for (int j = 0 ; j < index ; ++j )
		{
			n = file.Read(buff , 1024);        
			infile.Write(buff,n);              
		}
		infile.Close();        
	}
	file.Close();


 

当然在初始化里面的代码主要实现下拉框里面的内容

 

 

CString s;
 //添加默认的分割后文件的大小
 for (int i = 1 ; i <= 100 ; ++i )
 {
  s.Format("%dkb",i);
  m_com.InsertString(i - 1 , s);
 }
 //下拉框默认设置100kb
 m_com.SetCurSel(99);  


有的地方木有注释,看起来不是很好 ,这个是备份的,原本的被我删除了

 

代码在下面

 

 下载地址

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值