相关疑难解决方法(0)

如何使用C循环浏览文件夹中的所有文件?

我想从目录中的所有文件名中删除特定的子字符串:

- 来自'Futurama s1e20'的'XYZ.com' - [XYZ.com] .avi' -

所以基本上我需要为方法提供一个所需的子字符串,它必须循​​环遍历所有文件名并进行比较.

我无法弄清楚如何使用C循环遍历文件夹中的所有文件.

c

9
推荐指数
2
解决办法
3万
查看次数

使用 stat() 函数测试 DIRENT 是目录还是文件的正确方法是什么?

我在使用 'if(S_IFDIR(stbuf.st_mode))' 行时遇到一些问题。这是测试要递归到的目录的正确方法吗?目前该函数似乎可以正确执行 1 或 2 个循环,然后失败并出现分段错误。

我已经尝试过以下方法,并且可能更多作为条件。

S_ISDIR(st_mode)
((st_mode & ST_IFMT) == S_IFDIR)
S_IFDIR(stbuf.st_mode)
Run Code Online (Sandbox Code Playgroud)

我已经包含了整个函数,因为我担心问题可能出在其他地方。

void getFolderContents(char *source, int temp){
    struct stat stbuf;
    int isDir;
    dirPnt = opendir(source);
    if(dirPnt != NULL){
        while(entry = readdir(dirPnt)){
            char *c = entry->d_name;
            if(strcmp(entry->d_name, cwd) == 0 || strcmp(entry->d_name, parent) == 0){
            }
            else{
                stat(entry->d_name, &stbuf);
                printf("%i %i ", S_IFMT, stbuf.st_mode);
                if(S_IFDIR(stbuf.st_mode)){            //Test DIR or file
                printf("DIR: %s\n", entry->d_name);
                getFolderContents(entry->d_name, 0);
            }
            printf("FILE: %s\n", entry->d_name);
        }
    }
    closedir(dirPnt);
}
Run Code Online (Sandbox Code Playgroud)

c directory-structure function

5
推荐指数
1
解决办法
1万
查看次数

如何正确使用dirent.h

我是C++的新手,我正在尝试使用dirent.h头来操作目录条目.以下小应用程序编译后,你补充目录名称后呕吐.有人能给我一个暗示吗?int退出是为了提供while循环.我删除了循环以试图隔离我的问题.

谢谢!

#include <iostream>
#include <dirent.h>

using namespace std;

int main()
{

char *dirname = 0;
    DIR *pd = 0;
    struct dirent *pdirent = 0;

    int quit = 1;



    cout<< "Enter a directory path to open (leave blank to quit):\n";
    cin >> dirname;

    if(dirname == NULL)
    {
        quit = 0;

    }
        pd = opendir(dirname);

    if(pd == NULL)
    {
        cout << "ERROR: Please provide a valid directory path.\n";
    }


    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ dirent.h

1
推荐指数
2
解决办法
2万
查看次数

如何检查目录是文件还是文件夹?

好的,所以我使用的是mingW,直接结构没有名为d_type或stat,d_stat或dd_stat的变量.我需要知道如何使用我的直接结构来确定我所拥有的是文件或文件夹.这是我的代码.

#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
#include <vector>
#include <string>
#include <iostream>

using namespace std;

/*function... might want it in some class?*/
int getdir (string dir, vector<string> &files)
{
    DIR *dp;
    struct stat _buf;
    struct dirent *dirp;
    if((dp  = opendir(dir.c_str())) == NULL) {
        cout << "Error(" << errno << ") opening " << dir << endl;
        return errno;
    }

    while ((dirp = readdir(dp)) != NULL) {

        if(stat(dirp->d_name, &_buf) != 0x4)
        files.push_back(string(dirp->d_name));
    }
    closedir(dp);
    return 0;
}

int …
Run Code Online (Sandbox Code Playgroud)

c++ directory file dirent.h

1
推荐指数
1
解决办法
3万
查看次数

标签 统计

c ×2

c++ ×2

dirent.h ×2

directory ×1

directory-structure ×1

file ×1

function ×1