0

I'm using dirent.h to see all the files in var/log, and I want to save them in an array, but when I save them, it appears like numbers instead of a string. Why is this happening?

The code is here:

  char *word = ".gz" ; 
  char *word2 = ".";
  char *word3 = "..";
  DIR           *directory;
  struct dirent *dir;

  directory = opendir("/var/log");
  if (directory)  {
    while ((dir = readdir(directory)) != NULL)
    {
      if((strstr(dir->d_name, word) == NULL) && (strstr(dir->d_name, word2) == NULL) && (strstr(dir->d_name, word3) == NULL)) {
      printf("%s\n", dir->d_name);
      i++;
      }
    }
      printf("%d\n",i);

    closedir(directory);
  }
  char *array_of_files[i];
  i=0;
  directory = opendir("/var/log");
  if (directory)  {
    while ((dir = readdir(directory)) != NULL)
    {
      if((strstr(dir->d_name, word) == NULL) && (strstr(dir->d_name, word2) == NULL) && (strstr(dir->d_name, word3) == NULL)) {
      array_of_files[i]=dir->d_name;
      printf("%d array of file \n",array_of_files[i]);
      i++;
      }
    }
      printf("%d\n",i);
     int j;
     for (j=0;j<i;j++){

        printf("%d ", array_of_files[j]);
     }
    closedir(directory);

First I'm using a counter that counts the number of files in the folder, then I reopen the directory and create the array with that counter, and start saving the names of the files in the folder, but it shows only numbers.

An example of this:

./program

speech-dispatcher
dbconfig-common
apache2
mdm
fsck
samba
aptitude
dmesg
unattended-upgrades
postgresql
udev
installer
cups
hp
btmp
ConsoleKit
syslog
wtmp
mysql
lastlog
23
8962131 8962163 8962283 8962315 8962675 8962755 8962947 8963091 8963155 8963331 8963403 8963539 8963579 8963691 8963755 8964251 8964307 8964435 8964507 8964539 8964571 8964627 8964723

I know there's a better way of doing the array instead of two while loops, but I can't think of anything better right now; if you can recommend something it will be appreciated.

dfeuer
  • 48,079
  • 5
  • 63
  • 167
Hook
  • 391
  • 5
  • 16
  • If you read the [`readdir`](http://man7.org/linux/man-pages/man3/readdir.3.html) manual page you will see the paragraph "The data returned by readdir() may be overwritten by subsequent calls to readdir() for the same directory stream." That means you can't just copy the pointer to the name, you need to copy the actual string. – Some programmer dude Dec 26 '14 at 20:35

3 Answers3

2

Change below lines

printf("%d array of file \n",array_of_files[i]); to 
printf("%s array of file \n",array_of_files[i]);`

And

printf("%d ", array_of_files[j]); to 
printf("%s ", array_of_files[j]);
µtex
  • 900
  • 5
  • 12
2

Use the correct format as answered by @SAN.

Copying a pointer does not do much as dir->d_name may get repopulated between calls.
Code needs to create and manage a string copy.

char *array_of_files[i];
...
  while (...
    // array_of_files[i] =dir->d_name;
    array_of_files[i] = strdup(dir->d_name);
    ...
  }
}

for (j=0;j<i;j++){
  // printf("%d ", array_of_files[j]);
  printf("%s ", array_of_files[j]);
  free(array_of_files[j]);
}

If your system does not have strdup(), simple to create your own

Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

You should use

   printf("%c ", array_of_files[j]);