我在C中的stat函数有一个问题,我的应用程序必须列出两个目录中的所有文件(第二个目录还没有实现)。当dir1设置为“”时。对于当前目录,它会列出所有文件。如果我将其更改为所需的目录,它将只列出一个文件。
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
main ()
{
DIR *dir1;
DIR *dir2;
dir1 = opendir ("/home/tom/Documents/Uni/Dropbox/OS/C/1/");
dir2 = opendir ("/home/tom/Documents/Uni/Dropbox/OS/C/2/");
struct dirent *ent;
struct stat fileStat;
if (dir1 != NULL)
{
/* while there are files to read in the directory */
while ((ent = readdir (dir1)) != NULL)
{
/*printf ("In 1\n"); <--debugging--> */
if (stat(ent->d_name,&fileStat) == 0)
{
/* ignores . and .. and hidden files */
/* printf ("In 2\n"); <--debugging--> */
if(ent->d_name[0] != '.')
{
/* printf ("In 3\n"); <--debugging--> */
printf ("\n");
printf ("File: %s\n", ent->d_name);
printf ("File size: %d\n", fileStat.st_size);
printf ("-----------------------------------\n");
}
}
}
/* close the 1st directory */
closedir (dir1);
/* close the 2nd directory */
closedir (dir2);
}
else
{
/* prints an error if the directory can not be opened */
perror ("");
}
}程序运行结果如下:
tom@x60deb:~/Documents/Uni/Dropbox/OS/C$ ./ffffuuuuuu
File: ffffuuuuuu.c
File size: 1045
-----------------------------------这是它正在读取的目录中ls的结果:
tom@x60deb:~/Documents/Uni/Dropbox/OS/C/1$ ls -l
total 36
-rw-r--r-- 1 tom tom 356 Dec 12 23:36 cwTest2.c
-rw-r--r-- 1 tom tom 322 Dec 12 23:36 cwTest.c
-rw-r--r-- 1 tom tom 627 Dec 12 23:36 ffffuuuuuu.c
-rw-r--r-- 1 tom tom 6 Dec 12 23:32 file
-rw-r--r-- 1 tom tom 6 Dec 12 23:32 file2
-rw-r--r-- 1 tom tom 6 Dec 12 23:45 file2.file
-rw-r--r-- 1 tom tom 15 Dec 12 23:33 file3
-rw-r--r-- 1 tom tom 15 Dec 12 23:45 file3.file
-rw-r--r-- 1 tom tom 6 Dec 12 23:45 file.file提前谢谢你,汤姆。
发布于 2011-12-13 08:13:12
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
int main (void)
{
char * dirname = "/home/tom/Documents/Uni/Dropbox/OS/C/1/" ;
DIR *dir1;
char path[11111];
size_t len;
struct dirent *ent;
struct stat fileStat;
dir1 = opendir (dirname);
len = strlen ( dirname);
memcpy(path, dirname, len+1);
struct dirent *ent;
struct stat fileStat;
if (dir1 != NULL)
{
/* while there are files to read in the directory */
while ((ent = readdir (dir1)) != NULL)
{
/*printf ("In 1\n"); <--debugging--> */
strcpy(path+len, ent->d_name);
if (stat( path,&fileStat) == 0)
{
/* ignores . and .. and hidden files */
/* printf ("In 2\n"); <--debugging--> */
if(ent->d_name[0] != '.')
{
/* printf ("In 3\n"); <--debugging--> */
printf ("\n");
printf ("File: %s\n", ent->d_name);
printf ("File size: %d\n", fileStat.st_size);
printf ("-----------------------------------\n");
}
}
}
/* close the 1st directory */
closedir (dir1);
}
else
{
/* prints an error if the directory can not be opened */
perror ("");
}
return 0;
}更新因为有些人看不懂,我将在这里添加我的原始评论:
您当前的目录是什么?这些条目是相对于"/home/tom/Documents/Uni/Dropbox/OS/C/1/“(您应该给stat()提供完整的路径名)的:还有比”“更多的条目。”和"..“以".“开头。
发布于 2011-12-13 08:26:07
您必须将stat()的名称指定为绝对路径名或相对于当前目录的名称。
如果你的扫描仪改变了目录,那么(a)你比我更勇敢,(b)你可以使用短名称,但(c)你必须担心如何回到你开始的地方。
如果您使用的是POSIX2008,那么您也许能够使用系统调用的*at()变体来简化工作;但是我还不确定有多少(如果有的话)系统支持这些调用。
https://stackoverflow.com/questions/8482659
复制相似问题