Thomas Mitchell
Thomas Mitchell

Reputation: 1071

C stat() ignoring files

I'm having an issue with the stat function in C. My application must list all files in two directories (2nd directory not implemented yet). When dir1 is set to "." for current directory it lists all files. If I change it to the required directory it will only list one file.

#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 ("");
    } 
}

The result of running the program is below:

tom@x60deb:~/Documents/Uni/Dropbox/OS/C$ ./ffffuuuuuu 

File: ffffuuuuuu.c
File size: 1045
-----------------------------------

This is the result of ls in the directory it is reading:

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

Thanks in advance, Tom.

Upvotes: 1

Views: 1407

Answers (2)

wildplasser
wildplasser

Reputation: 44230

#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; 
}

UPDATE because some people cannot read, I'll add my original comment here:

what is your current directory? the entries are relative to "/home/tom/Documents/Uni/Dropbox/OS/C/1/" (you should give stat() the full pathname) ALSO: there are more entries than "." and ".." that start with ".".

Upvotes: 0

Jonathan Leffler
Jonathan Leffler

Reputation: 753475

You have to specify the name to stat() either as an absolute path name or as the name relative to the current directory.

If your scanner is changing directory then (a) you're a braver man than I am, and (b) you can use the short names, but (c) you have to worry about how to get back to where you started.

If you have POSIX 2008, you may be able to use the *at() variants of the system calls to simplify life; but I'm not sure how many (if any) systems support those calls yet.

Upvotes: 1

Related Questions