Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6927787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:06:02+00:00 2026-05-27T11:06:02+00:00

I’m having an issue with the stat function in C. My application must list

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:06:03+00:00Added an answer on May 27, 2026 at 11:06 am
    #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 “.”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I need a function that will clean a strings' special characters. I do NOT
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from
I want to construct a data frame in an Rcpp function, but when I
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have thousands of HTML files to process using Groovy/Java and I need to
I have a bunch of posts stored in text files formatted in yaml/textile (from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.