c语言读取文件夹下所有的文件名

baishen2484 2017-07-05 03:20:55
比如文件夹G:\\chengxu\\Image,,里面有n张图片1.jpg,2.jpg.....遍历文件夹中所有图片名,保存到char **Files中,要包含全部路径
...全文
2614 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2017-07-05
  • 打赏
  • 举报
回复
system("dir /b /a-d c:\\*.* >d:\\allfiles.txt"); //读文件d:\\allfiles.txt的内容即C:\\下所有文件的名字 system("dir /b /a-d /s c:\\*.* >d:\\allfilesinsub.txt"); //读文件d:\\allfilesinsub.txt的内容即C:\\下所有文件的名字包含子目录 system("dir /b /ad c:\\*.* >d:\\alldirs.txt"); //读文件d:\\alldirs.txt的内容即C:\\下所有子目录的名字 请记住,能用shell命令获取文件、文件夹信息或者操作文件、文件夹最好用shell命令获取或者操作,而不要用各种API获取或者操作,因为当遇到非法文件夹名或非法文件名或非法文件长度、非法文件日期、压缩文件、链接文件、稀疏文件……等各种意料之外的情况时,API会处理的不全面或陷入死循环,而shell命令不会。 如果嫌system黑窗口一闪,将system("...")替换为WinExec("cmd /c ...",SW_HIDE);
自信男孩 2017-07-05
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>
#include <dirent.h>

int main(int argc, char *argv[])
{
    if (argc < 2) {
        fprintf(stdout, "Usage: too few paramters!\n");
        return 0;
    }

    DIR *dp = NULL;

    dp = opendir(argv[1]);
    if (!dp) {
        fprintf(stderr, "opendir: %s\n", strerror(errno));
        return 0;
    }

    struct dirent *dirp;
    while ((dirp = readdir(dp))) {
        if (strcmp(dirp->d_name, ".") == 0 || strcmp(dirp->d_name, "..") == 0)
            continue;
        printf("%s\n", dirp->d_name);
    }
    closedir(dp);

    return 0;
}
系统调用,建议把上面的代码先运行一下,然后改成你想要的--就是把文件名拷贝到你要得缓存里。
Youth1996 2017-07-05
  • 打赏
  • 举报
回复 2

@echo off & setlocal EnableDelayedExpansion
for /f "delims=" %%i in ('"dir /a/s/b/on *.*"') do (
set file=%%~fi
set file=!file:/=/!
echo !file! >> 路径.txt
)
bat文件内输入这个,然后把改文件放在你需要的目录下
Youth1996 2017-07-05
  • 打赏
  • 举报
回复
这个批处理 dos就可以做到吧

70,020

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧