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 255197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:54:38+00:00 2026-05-11T21:54:38+00:00

Given a path, say, /home/shree/path/def, I would want to determine if def is a

  • 0

Given a path, say, /home/shree/path/def, I would want to determine if def is a directory or a file. Is there a way of achieving this in C or C++ code?

  • 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-11T21:54:38+00:00Added an answer on May 11, 2026 at 9:54 pm

    The following code uses the stat() function and the S_ISDIR (‘is a directory’) and S_ISREG (‘is a regular file’) macros to get information on the file. The rest is just error checking and enough to make a complete compilable program.

    #include <stdio.h>
    #include <errno.h>
    #include <sys/stat.h>
    
    int main (int argc, char *argv[]) {
        int status;
        struct stat st_buf;
    
        // Ensure argument passed.
    
        if (argc != 2) {
            printf ("Usage: progName <fileSpec>\n");
            printf ("       where <fileSpec> is the file to check.\n");
            return 1;
        }
    
        // Get the status of the file system object.
    
        status = stat (argv[1], &st_buf);
        if (status != 0) {
            printf ("Error, errno = %d\n", errno);
            return 1;
        }
    
        // Tell us what it is then exit.
    
        if (S_ISREG (st_buf.st_mode)) {
            printf ("%s is a regular file.\n", argv[1]);
        }
        if (S_ISDIR (st_buf.st_mode)) {
            printf ("%s is a directory.\n", argv[1]);
        }
    
        return 0;
    }
    

    Sample runs are shown here:

    
    pax> vi progName.c ; gcc -o progName progName.c ; ./progName
    Usage: progName 
           where  is the file to check.
    
    pax> ./progName /home
    /home is a directory.
    
    pax> ./progName .profile
    .profile is a regular file.
    
    pax> ./progName /no_such_file
    Error, errno = 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a path for a given file, say 'C:\Folder\OtherFolder\AnotherFolder\file.txt' , is there
I would like to query an XDocument object for a given path, (e.g. /path/to/element/I/want)
Given a DFS path, how can I programatically determine which physical file server it
Is there any way to say that if prerequisite for the given target doesn't
Given this directory tree: src/MyLibrary/MyHeader.h src/file.cpp file.cpp: #include mylibrary/myheader.h ... Compiling file.cpp works with
How to check if target of a given path is a directory? Say: rem
Lets say I have an Areas Table ( id, parentId, name, path ). Given
Given the path of a file (e.g. C:\MyPicture.jpg), how do I convert a binary
Documentation states: Files are included based on the file path given or, if none
Given a file path such as: \\server\folder_A\folder_B\etc\more.mov I need a regex that'll give me

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.