2011-04-03 28 views
0

嗨 我想编写一个程序,在我的系统中提供一个路径并转到该路径并搜索该路径和路径的子目录,并列出所有.txt文件。 请帮助我。 谢谢。所有.txt文件的列表

+1

那么你在问什么,C,C++或Qt?每个人都会使用不同的方法。用于C语言的系统函数,用于C++和Qt的boost在内部提供。 – 2011-04-03 10:35:08

回答

2

以下代码将列出“C:\ Windows \ System32”目录中的所有文件(适用于XP系统的SYSDIR)。不管你想要什么,都可以将它添加到代码中

DIR *dir; 
struct dirent *ent; 
     dir = opendir ("c:\\Windows\\System32"); 
if (dir != NULL) { 

    /* print all the files and directories */ 
    while ((ent = readdir (dir)) != NULL) { 
    printf ("%s\n", ent->d_name); 
    } 
    closedir (dir); 
} else { 
    /* Can not open directory */ 
    perror (""); 
    return EXIT_FAILURE; 
} 
+0

此解决方案不关注通配符 – 2011-04-03 10:28:31

+0

使目录变为参数并递归调用函数以处理子目录。并且将迪尔传递给错误。 – 2011-04-03 10:30:32

+0

我正在使用Visual Studio 2010。我怎样才能使用dirent.h? – woody 2011-04-03 11:02:08

0

创建一个QDir类型的对象。这可以用于导航到所需的文件夹,例如通过使用dirobj.cd("/mypath/somewhere")。然后使用dirobj.entryList(QStringList("*.txt", "*.otherext", ...)检索找到的文件列表。