0

I'm using readdir() to read to output all the files in a directory. The problem is that I need to save the strings into a buffer. Is there any way to save the output into a buffer or file descriptor etc?

Here is my code:

  DIR *directory;
  struct dirent *dir;

  directory = opendir();

  while ((dir = readdir(directory)) != NULL) {
    printf("%s\n", dir->d_name);
  }

  closedir(directory);
P.P
  • 117,907
  • 20
  • 175
  • 238
user16655
  • 1,901
  • 6
  • 36
  • 60
  • Yes there is. But you should tell us a bit more of what you want to do exactly. – Jabberwocky Nov 17 '14 at 13:46
  • You can copy the strings into a buffer instead of printing it. – P.P Nov 17 '14 at 13:57
  • It's a part of a server/client communication, and I want to save it into a buffer of a certain size (potentially several if the input is great enough), and then send it. I have no idea on how to save the input, that's why I asked :) – user16655 Nov 17 '14 at 14:14

3 Answers3

0

In this case you will have to work with a data structure, like a linked list or a tree. Here's a short example, an idea to work on:

 pDir = readdir(directory);
 while ( pDir != NULL ){
   strcpy(dirStruct[iCtFile]->FileName, pp->d_name);
 }

Looking ahead, you should consider path treatment and other issues.

rfermi
  • 179
  • 11
0

Note that you'll need to pass a directory name to opendir().

This is simple example of how to read and save them into a buffer. It doubles the pointers every time it hits the limit. Modern operating systems will clean up the memory allocated once process dies. Ideally, you should also call free() in case of failures.

#include<stdio.h>
#include <dirent.h>
#include <sys/types.h>
#include<string.h>
#include<stdlib.h>

int main(void)
{
  size_t i = 0, j;
  size_t size = 1;
  char **names , **tmp;
  DIR *directory;
  struct dirent *dir;

  names = malloc(size * sizeof *names); //Start with 1

  directory = opendir(".");
  if (!directory) { puts("opendir failed"); exit(1); }

  while ((dir = readdir(directory)) != NULL) {
     names[i]=strdup(dir->d_name);
     if(!names[i]) { puts("strdup failed."); exit(1); }
     i++;
     if (i>=size) { // Double the number of pointers
        tmp = realloc(names, size*2*sizeof *names );
        if(!tmp) { puts("realloc failed."); exit(1); }
        else { names = tmp; size*=2;  }
     }
  }

  for ( j=0 ; j<i; j++)
  printf("Entry %zu: %s\n", j+1, names[j]);

  closedir(directory);
}
P.P
  • 117,907
  • 20
  • 175
  • 238
0

Use scandir How to use scandir

It allocates the memory for you. Here is an example

/**code to print all the files in the current directory **/
struct dirent **fileListTemp;
char *path = ".";//"." means current directory , you can use any directory path here
int noOfFiles = scandir(path, &fileListTemp, NULL, alphasort);
int i;
printf("total: %d files\n",noOfFiles);
for(i = 0; i < noOfFiles; i++){
    printf("%s\n",fileListTemp[i]->d_name);

you can modify it to suit your need . Also do not forget to free memory allocated by scandir

Community
  • 1
  • 1
mchouhan_google
  • 1,775
  • 1
  • 20
  • 28
  • scandir() has 5 parameters, not 4. as in: int scandirat( int dirfd, const char *dirp, struct dirent ***namelist, int (*filter)(const struct dirent *), int (*compar)(const struct dirent **, const struct dirent **)); – user3629249 Nov 17 '14 at 15:30
  • I have tried to find a solution on how to save the output from this method into a char buffer[]. I have used strcat, and this works, but I think I read somewhere that it's a bad solution? Is this correct? – user16655 Nov 17 '14 at 21:49
  • Also, one more question! Is it possible to call scandir from a specified directory? – user16655 Nov 17 '14 at 21:59
  • are you trying to store all the elements in single buffer ? – mchouhan_google Nov 17 '14 at 22:34
  • Yeah, or (potentially) several if the size is bigger than 500. – user16655 Nov 18 '14 at 19:05