У меня проблема с функцией stat в C. Мое приложение должно перечислить все файлы в двух каталогах (2-й каталог еще не реализован). Когда 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
Спасибо заранее,
Том.