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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:36:04+00:00 2026-06-13T16:36:04+00:00

I need to fill a buffer space with file descriptors of files from a

  • 0

I need to fill a buffer space with file descriptors of files from a defined source directory. So I have the startup code:

int main(int argc, char* argv[])
{
    DIR *src=opendir(argv[1]);

    struct dirent *DirEntry;
    char* buffer[200];
    do {
        DirEntry = readdir(src);
        if(DirEntry != NULL) {
            //put file into buffer
        }
    }while(DirEntry!=NULL);
}

How do I complete this loop to place all file descriptors of a given directory into the array called ‘buffer’? Should I use an object of DirEntry like DirEntry->d_name to return a file descriptor that I then put into the array?

  • 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-06-13T16:36:05+00:00Added an answer on June 13, 2026 at 4:36 pm

    If you need to move files from a source directory to a destination directory, you are going to need file names much more than you need file descriptors. With the names, you can open and close descriptors whenever needed; without the names, you can’t create the files in the target directory sensibly. However, we can handle file descriptors too.

    So, assuming you have strdup(), you might use:

    typedef struct File
    {
        char *name;
        int   fd;
    } File;
    

    And in your loop:

    if (DirEntry != NULL)
    {
        buffer[i].name = strdup(DirEntry->d_name);
        if (buffer[i].name != 0)
            buffer[i].fd = open(buffer[i].name, O_RDONLY);
        i++;
    }
    

    where buffer is an array of File and i is a convenient integer:

    enum { MAX_FILES = 4096 };
    int i;
    File buffer[MAX_FILES];
    

    You should also add a condition to the main condition to ensure no overflow (or replace the fixed size buffer with a dynamically allocated one):

    if (DirEntry != NULL && i < MAX_FILES)
    

    You could sensibly break the loop if i reaches the limit. You could test whether the name represents a file (as opposed to FIFO, block device, character device, socket, symlink, directory, …); you’d probably use stat() or lstat() for that. The file descriptor would be negative (-1) if the open() call failed. You might conserve entries by not incrementing i if the memory allocation fails, but it is probably not worth worrying about. If the memory allocation for a file name fails, there isn’t going to be much else that works.

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

Sidebar

Related Questions

I need to code for uploading a file from client to server. In client
I have a List[Option[MyClass]] with None in random positions and I need to 'fill'
I have two fields that need to multiply each other and fill a third
I have custom model file format that I am reading from to create a
at the moment I have a small application and need to take information from
I have a list of files, which need to be read, in chunks, into
StackOverflow people, Need Java Advice and Code which reads a file(File.txt) and prints to
What I need to do is to fill the entire file contents with zeros
I need to fill textboxes depending on the item selected in a combobox. I
I need to fill a rectangle with a black to white (transparent) gradient. However,

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.