首先,我是编程方面的完全新手,尤其是C语言。我将要展示的示例是我从此网站获取的代码,并且 已被踢掉,直到它没有我完全不想做我想做的事。 :)在GNU清单上跳过目录C
其次,我正在使用Linux Mint和GNU C编译器的双核Celeron。
我想要做的是列出目录中的子目录,跳过父目录(如果有的话)。不幸的是,我有 的代码不起作用。我尝试了strcpy和strncpy,但我无法弄清楚如何使用它们,我只是搞砸了代码,以致于在尝试运行程序时出现 “分段错误”。
下面的代码:
/*
* This program displays the names of all files in the current directory.
*/
#include <dirent.h>
#include <string.h>
#include <stdio.h>
int main(void)
{
DIR *d;
struct dirent *dir;
char dirname[255];
d = opendir(".");
if (d)
{
while ((dir = readdir(d)) != NULL)
{
if (dir->d_type == DT_DIR)
{
strncpy(dirname, dir->d_name, 254);
dirname[254] = '\0';
if (dirname != "..") /* I want to skip this, but it doesn't work */
{
printf ("%s\n", dirname);
}
}
}
closedir(d);
}
return(0);
}
感谢所有帮助你可以给。我特别感兴趣的是可以提供教程的网站链接,这些教程可以帮助我完成这个小项目,因为我希望更好地理解我正在做什么。 :)
'如果(DIR-> d_type == DT_DIR){如果(的strcmp(DIR-> d_name “.. ”)!= 0){printf的(“ %S \ n” 个,DIR-> d_name ); }}' – melpomene
如果你想了解你在做什么,从头开始! – t0mm13b
可能的重复[如何比较字符串在C条件](http://stackoverflow.com/questions/14779112/how-to-compare-strings-in-c-for-a-condition) – usr2564301