使用C打开目录

2024-01-25

我接受通过命令行输入的路径。

当我做

dir=opendir(args[1]);

它不会进入循环...即dir==null...

如何将命令行输入传递给 dir 指针?

void main(int c,char **args)
{
    DIR *dir;
    struct dirent *dent;
    char buffer[50];
    strcpy(buffer, args[1]);
    dir = opendir(buffer);   //this part
    if(dir!=NULL)
    {
        while((dent=readdir(dir))!=NULL)
            printf(dent->d_name);
    }
    close(dir);
}

./a.out  /root/TEST is used to run the program..
./a.out --> to execute the program
/root/TEST --> input by the user i.e valid path

You should really post your code(a), but here goes. Start with something like:

    #include <stdio.h>
    #include <dirent.h>

    int main (int argc, char *argv[]) {
        struct dirent *pDirent;
        DIR *pDir;

        // Ensure correct argument count.

        if (argc != 2) {
            printf ("Usage: testprog <dirname>\n");
            return 1;
        }

        // Ensure we can open directory.

        pDir = opendir (argv[1]);
        if (pDir == NULL) {
            printf ("Cannot open directory '%s'\n", argv[1]);
            return 1;
        }

        // Process each entry.

        while ((pDirent = readdir(pDir)) != NULL) {
            printf ("[%s]\n", pDirent->d_name);
        }

        // Close directory and exit.

        closedir (pDir);
        return 0;
    }

您需要检查您的情况args[1]既已设置又引用实际目录。示例运行,tmp是我当前目录之外的子目录,但您可以使用任何有效的目录,给我: 测试程序临时文件

[.]
[..]
[file1.txt]
[file1_file1.txt]
[file2.avi]
[file2_file2.avi]
[file3.b.txt]
[file3_file3.b.txt]

另请注意,您必须通过目录在,不是一个file.当我执行时:

testprog tmp/file1.txt

I get:

Cannot open directory 'tmp/file1.txt'

这是因为它是一个文件而不是一个目录(不过,如果你偷偷摸摸,你可以尝试使用diropen(dirname(argv[1]))如果初始的diropen fails).


(a) This has now been rectified but, since this answer has been accepted, I'm going to assume it was the issue of whatever you were passing in.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用C打开目录 的相关文章

随机推荐

  • lapply 并应用于列表 R 的每个组件和元素

    我有这个清单 lst lst lt list a c 2 5 9 8 5 0 6 7 6 5 5 2 34 4 4 2 39 5 1 3 0 0 0 0 4 1 0 0 0 0 25 5 196 5 0 0 104 2 0 0 0 0 0
  • 在 Excel 宏中设置图表坐标、标题和添加文本的代码格式建议

    编写了一个宏 用 Excel 绘制带有标记的线 它在单个宏 xlsm 文件中运行良好 但是当我尝试将其转换为 Excel 插件 xlam 文件 时 它出现了很多错误 所有错误都与 X 和 Y 坐标的格式 图表标题的位置 字体类型和大小以及添
  • 可以在引导之前使用自定义 Angular 服务吗?

    我有一个服务 angular module USC service TemplateService function 我想在手动引导我的 Angular 项目之前使用它 angular bootstrap document USC 这可能吗
  • 量角器片状

    我维护一个复杂的 Angular 1 5 x 应用程序 正在使用 Protractor 2 5 x 对其进行 E2E 测试 我在使用这种方法时遇到了一个问题 主要表现为测试看起来不稳定 在一个拉取请求中运行良好的测试在另一拉取请求中失败 这
  • 在 NestJS 中添加 HttpRequest 标头

    我正在尝试在 NestJS 中发出 Http 请求 因为它的灵感来自 Angular 所以我添加了我的标题 import Injectable HttpService from nestjs common const headersRequ
  • JS等待获取=> [对象响应]

    我有这个问题 API Blynk cc 有 2 个输出可能 TRUE 或 FALSE 简单文本 无 JSON 访问 https ip port APIKey isHardwareConnected 时 我得到 TRUE 或 FALSE 当我
  • 从 InternetAddress 打印发件人的电子邮件地址

    这是获取的代码sender和subject使用此代码 我看到显示了正确的主题 但我看到发件人的地址以不同的格式显示 Properties props new Properties props put mail imap host imap
  • 尝试在 AWS Lambda 中运行 Flask 应用程序时出现错误

    我正在尝试使用 AWS Lambda 部署一个在本地运行良好的 Flask 应用程序severless 部署很好 但我无法使用 aws lambda app 我收到一个错误502 Bad Gateway message Internal s
  • 如何生成 yyyymm 值范围? [复制]

    这个问题在这里已经有答案了 我有两个将由用户输入的 yyyymm 值 yyyymm 1 201406 yyyymm 2 201501 我希望能够以递增的月份顺序迭代此范围 for yyyy and mm in the range of yy
  • 如何修复非法指令(核心转储)

    您好 我正在尝试解决此问题 当我运行下面的 python3 Brain py 时 出现此错误 非法指令 核心转储 from imageai Prediction import ImagePrediction import os execut
  • 生产中的 Create-React-App:找不到路由

    我有一个以 Create React App 和 Express js 作为后端的全栈应用程序 开发设置 CRA 在端口 3000 上运行 是由 CRA 的代理实现的 因此我可以将某些路由直接转发到后端 在端口 5000 上运行 proxy
  • 通过notifyDataSetChanged() 没有更改/黑屏;

    我想做这样的事情 同时按下refresh按钮 它将刷新列表 活动代码如下 adapter new TweetAdapter Welcome this tweets users tweetsList setAdapter adapter pr
  • 这个语法是什么意思? (序言)

    我一直在尝试学习 Prolog 并在一些示例代码中遇到了这种语法 solve Hs Hs member h dog Hs 这只是代码的一部分 但我对h dog does 任何帮助将不胜感激 下划线 只是表明该位置有一个值 但我们不关心它 第
  • 使用 BeginInvoke 和 EndInvoke 时,如何避免必须传递/存储委托?

    Edit 将实际问题移至顶部 Update 找到了微软的一个例子 最后添加了一些代码 我的问题是 在同一个委托实例上调用多个 BeginInvoke 调用是否安全 或者我是否必须为每个正在进行的方法调用构造一个新的委托实例 如果我必须为每个
  • Java 8 扩展流

    我正在尝试扩展 Java 8 的 Stream 实现 我有这个界面 public interface StreamStuff
  • git pull 导致“文件取消链接失败”错误

    我正在尝试做一个git pull我遇到了可怕的 取消文件 xxx 链接失败 错误 我已经尝试过 ProcessExplorer 中定义的this https stackoverflow com a 6076796 341611回答 使用 f
  • 访问 ActiveDirectory 的代码的单元测试

    对访问 ActiveDirectory 的应用程序进行单元测试和 或模拟对 AD 的依赖关系的最佳方法是什么 所有必需的类型 例如DirectorySearcher and DirectoryEntry看起来并不容易被嘲笑 我们有类似的方法
  • 使用 htaccess 文件删除 html 扩展名

    我知道这是一个非常常见的话题 但没有一个解决方案适合我 我尝试过几个答案 answer 1 https stackoverflow com questions 15728888 how do i remove html extensions
  • C++ 指针数组:删除还是删除[]?

    考虑以下代码 class Foo Monster monsters 6 Foo for int i 0 i lt 6 i monsters i new Monster virtual Foo 什么是正确的析构函数 this Foo Foo
  • 使用C打开目录

    我接受通过命令行输入的路径 当我做 dir opendir args 1 它不会进入循环 即dir null 如何将命令行输入传递给 dir 指针 void main int c char args DIR dir struct diren