vc遍历文件夹

#include <afx.h>
#include <afxwin.h> //   MFC   core   and   standard   components  
#include <afxext.h>

///MFC头文件须在前

#include <fstream>
#include <iostream>
#include <stdio.h>
#include <string>

#define INPUTPATHSTR "corpus//*.txt"
#define OUTPUTPATHSTR "result"

using namespace std;
int main()
{
   string szFileNum,infileName,outfileName,strline;
CFileFind finder;   //读所有的文件  
BOOL bWorking = finder.FindFile(INPUTPATHSTR);
// ifstream in ; (infileName.c_str());
// ofstream out; (outfileName.c_str());
while (bWorking)
{  
   bWorking = finder.FindNextFile();  
   if(finder.IsDots())  
    continue;  
   if(!finder.IsDirectory())  
   {  
    CString CInputFileStr = finder.GetFilePath();//得到文件名
    int NamePos = CInputFileStr.ReverseFind('//');
    infileName = (LPCTSTR)CInputFileStr;
    szFileNum = infileName.substr(NamePos, infileName.length() - NamePos + 1);
            outfileName=OUTPUTPATHSTR+szFileNum;
      ifstream in (infileName.c_str());
      ofstream out(outfileName.c_str());
    while (getline(in,strline))
    {
               out<<strline<<endl;
    }
    cout<<szFileNum<<" is processed!"<<endl;
             in.close();
    out.close();
   }
}
finder.Close();
     return 0;
}

***************************************************************/

 

void CLiuChunHongDlg::OnButton1()
{
// TODO: Add your control notification handler code here
CString   strPath;
BROWSEINFO   bi   ={0};
char   pszDisplayName[MAX_PATH]   ="/0";
     bi.hwndOwner=NULL;  
bi.pszDisplayName   =   pszDisplayName;
bi.lpszTitle="请选择目录:";
     bi.ulFlags=BIF_RETURNONLYFSDIRS;
LPITEMIDLIST   lpIL   =   SHBrowseForFolder(&bi);
if(lpIL)
{  
   SHGetPathFromIDList(lpIL,bi.pszDisplayName);   
}      
else
{     
   strPath   =   _T("");
}      
strPath   = bi.pszDisplayName;
m_path=strPath;
UpdateData(FALSE);
}

void CLiuChunHongDlg::OnOK()
{
// TODO: Add extra validation here
CDialog::OnOK();
std::string szFileNum;
std::string strline;
    std::string str;
int i;
CString houzhui="//*.txt";
CString INPUTPATHSTR=m_path+houzhui;
    CFileFind   finder;   //读所有的文件  
BOOL bWorking = finder.FindFile(INPUTPATHSTR);
// CString out_path="F://马龙//mycode//LiuChunHong//corpus2";
CStdioFile txt_cn,m_out;
while (bWorking)
{  
   CString out_path="F://马龙//mycode//LiuChunHong//corpus2";
   bWorking = finder.FindNextFile();  
   if(finder.IsDots())  
    continue;  
   if(!finder.IsDirectory())  
   {  
    CString CInputFileStr = finder.GetFilePath();//得到文件名
    int NamePos = CInputFileStr.ReverseFind('//');
    szFileNum = (LPCTSTR)CInputFileStr;
    szFileNum = szFileNum.substr(NamePos, szFileNum.length() - NamePos + 1);
      const char* ptr=szFileNum.c_str();
            CString b=ptr;
            out_path=out_path+b;
    char *out_path1=out_path.GetBuffer(out_path.GetLength());
        char *cn_path = CInputFileStr.GetBuffer(CInputFileStr.GetLength());
        txt_cn.Open(cn_path, CStdioFile::modeReadWrite);
            m_out.Open(out_path1, CStdioFile::modeReadWrite|CStdioFile::modeCreate);
        CString line_cn;
            while(txt_cn.ReadString(line_cn))
     {
     //m_out.SeekToEnd();  
     if (!line_cn.IsEmpty())
     {
           i=line_cn.Find(" ");//find(" ");
           //j=line_cn.GetLength();
     line_cn.Delete(0,i+1);
           m_out.WriteString(line_cn);
     m_out.WriteString("/n");
     }
              else
     {
             m_out.WriteString("/n");
     // out<<endl;
     }
              //m_out.WriteString(line_cn);
              //m_out.WriteString("/n");
     }
          
         m_out.Close();
    txt_cn.Close();
   }
}
  
finder.Close();
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值