c++遍历目录文件

博客提出为何80%的码农做不了架构师的问题,还提到存在对中文目录不支持的情况待优化,内容转载自https://my.oschina.net/guyson/blog/308648 。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <io.h>
#include <windows.h>
using namespace std;
 
void getFileList(const string& sPath,vector<string>& fileList);
 
void getFile(const string& sPath,vector<string>& fileList,_finddata_t& file)
{
    if(file.attrib == _A_SUBDIR)
    {
        string filename = file.name;
        if(filename == "." || filename == "..")
        {
            return ;
        }
        string sAddPath = sPath;
        sAddPath += filename;
        sAddPath += "\\";
        getFileList(sAddPath,fileList);
    }
    else
    {
        string sAddPath = sPath;
        sAddPath += file.name;
        fileList.push_back(sAddPath);
    }
}
 
void getFileList(const string &sPath,vector<string>& fileList)
{  
    struct _finddata_t file;
	string sPathLast, sPath2 = sPath;
    long hFile;

	if ( sPath[sPath.length()-1] != '\\' )
		sPath2 += "\\";

	sPathLast = sPath2 + "*";
    hFile = _findfirst(sPathLast.c_str(), &file);
    if(hFile == -1)
        return;
    else
        getFile(sPath2,fileList,file);
 
    while(_findnext(hFile, &file) != -1)
    {
        getFile(sPath2,fileList,file);
    }
 }
 
int main(int argc,char** argv)
{
		string fileDir("E:");
		vector<string> rfileList;

		getFileList(fileDir,rfileList);
 		for(size_t i=0;i<rfileList.size();i++)
        {
            cout<<rfileList[i]<<endl;
        }

		system("pause");
		return 0;
}


发现一个问题, 对中文目录不支持。。。待优化



转载于:https://my.oschina.net/guyson/blog/308648

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值