简体   繁体   English

C ++遍历文件和目录

[英]C++ iterating through files and directories

I'm working on a C++ program that will automatically backup my work to my FTP server. 我正在使用一个C ++程序,它将自动将我的工作备份到我的FTP服务器上。 So far I am able to upload a single file, by specifying a file name using this 到目前为止,我可以通过使用以下命令指定文件名来上传单个文件

CString strFilePath = szFile ;
            int iPos = strFilePath.ReverseFind('\\');
            CString strFileName = strFilePath.Right((strFilePath.GetLength()- iPos-1) );

            CString strDirPath = m_szFolderDroppedIn ;
            strDirPath = strDirPath.Mid(0,strDirPath.GetLength() - 1);  
            int iPost = strDirPath.ReverseFind('\\');
            CString strDirName = strDirPath.Right((strDirPath.GetLength()- iPost -1) );

            bool curdir = ftpclient.SetServerDirectory((char*)strDirName.GetBuffer(strDirName.GetLength())); 

            //Upload to Server 
            int uploadret = ftpclient.PutFile(szFile,(char*)strFileName.GetBuffer(strFileName.GetLength()),0,true,dwLastError); 
            m_lsDroppedFiles.RemoveAll();
            break;
            }

Now I want to be able to iterate through a directory (Which contains subdirectories) and recursively call. 现在,我希望能够遍历目录(包含子目录)并递归调用。 I'm having a problem getting a hold of the files in the directory. 我在保留目录中的文件时遇到问题。

Any help or code snippet... 任何帮助或代码段...

Since you are using MFC, you can use the CFileFind class. 由于使用的是MFC,因此可以使用CFileFind类。 Example code is given in MSDN. 示例代码在MSDN中给出。 Alternatively, you can use boost.filesystem for the same. 另外,您也可以使用boost.filesystem

@Swapnil:如果使用boost::filesystem ,则有一个recursive_directory_iterator

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM