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

  • SEARCH
  • Home
  • 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 1030829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T13:49:57+00:00 2026-05-16T13:49:57+00:00

I am accepting the path through command line input. When I do dir=opendir(args[1]); it

  • 0

I am accepting the path through command line input.

When I do

dir=opendir(args[1]);

it doesn’ t enter the loop…i.e dir==null…

How do I pass the command line input to dir pointer?

void main(int c,char **args)
{
    DIR *dir;
    struct dirent *dent;
    char buffer[50];
    strcpy(buffer, args[1]);
    dir = opendir(buffer);   //this part
    if(dir!=NULL)
    {
        while((dent=readdir(dir))!=NULL)
            printf(dent->d_name);
    }
    close(dir);
}

./a.out  /root/TEST is used to run the program..
./a.out --> to execute the program
/root/TEST --> input by the user i.e valid path
  • 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-16T13:49:57+00:00Added an answer on May 16, 2026 at 1:49 pm

    You should really post your code(a), but here goes. Start with something like:

        #include <stdio.h>
        #include <dirent.h>
    
        int main (int argc, char *argv[]) {
            struct dirent *pDirent;
            DIR *pDir;
    
            // Ensure correct argument count.
    
            if (argc != 2) {
                printf ("Usage: testprog <dirname>\n");
                return 1;
            }
    
            // Ensure we can open directory.
    
            pDir = opendir (argv[1]);
            if (pDir == NULL) {
                printf ("Cannot open directory '%s'\n", argv[1]);
                return 1;
            }
    
            // Process each entry.
    
            while ((pDirent = readdir(pDir)) != NULL) {
                printf ("[%s]\n", pDirent->d_name);
            }
    
            // Close directory and exit.
    
            closedir (pDir);
            return 0;
        }
    

    You need to check in your case that args[1] is both set and refers to an actual directory. A sample run, with tmp is a subdirectory off my current directory but you can use any valid directory, gives me:
    testprog tmp

    [.]
    [..]
    [file1.txt]
    [file1_file1.txt]
    [file2.avi]
    [file2_file2.avi]
    [file3.b.txt]
    [file3_file3.b.txt]
    

    Note also that you have to pass a directory in, not a file. When I execute:

    testprog tmp/file1.txt
    

    I get:

    Cannot open directory 'tmp/file1.txt'
    

    That’s because it’s a file rather than a directory (though, if you’re sneaky, you can attempt to use diropen(dirname(argv[1])) if the initial diropen fails).


    (a) This has now been rectified but, since this answer has been accepted, I’m going to assume it was the issue of whatever you were passing in.

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

Sidebar

Related Questions

we are accepting all sorts of national characters in UTF-8 string on the input,
I wrote a command line utility using Zend Framework to do some nightly reporting.
I know the solid security recommendation of avoiding accepting user input that you then
I am accepting form submissions. To prevent spoofing of the form I am using
[EDIT] I am NOT accepting any answer which involves BigInteger, or other similarly inefficient
I'm accepting dates from a wicket form - now I want query my DAO
I have a REST endpoint accepting a POST request to mark a code as
I want a regular expression for only accepting 0-9999 No spaces, no letters. However,
I have to use scanf() for accepting name of user in NSString object. If
I would like to have my script accepting variable arguments. How do I check

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.