vc遍历文件夹

来源:互联网 发布:cctv网络直播电视台 编辑:程序博客网 时间:2024/06/06 05:19

#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();
}

 

原创粉丝点击