windows与linux遍历文件

原创
小哥 2年前 (2023-05-25) 阅读数 69 #大杂烩

windows按文件名排序

#include 
    void GetFiles(string strPath, vector& vec)
    {
        strPath += "*.csv";
        _finddata_t fileInfo;
        long long handle = _findfirst(strPath.c_str(), &fileInfo);

        if (handle == -1L)
        {
            return ;
        }

        do
        {
            string str = fileInfo.name;
            vec.push_back(str);
        } while (_findnext(handle, &fileInfo) == 0);

        _findclose(handle);
    }

Linux 自然序

void GetFiles1(string strPath, vector& vec)
    {
        struct dirent* direntp;
        DIR* dirp = opendir(strPath.c_str());
        if (dirp == NULL)
        {
            return;
        }

        while ((direntp = readdir(dirp)) != NULL)
        {
            vec.push_back(direntp->d_name);
        }
        closedir(dirp);
    }

Linux 文件名排序

 void GetFiles2(string strPath, vector& vec)
    {
        struct dirent** namelist;
        int n = scandir(strPath.c_str(), &namelist,NULL, alphasort);
        if (n == -1)
        {
            return;
        }

        int i = 0;
        while (id_name[0]!=.)
            {
                vec.push_back(namelist[i]->d_name);
            }

            i++;
        }

        free(namelist);
    }
版权声明

所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除

热门