简体   繁体   中英

C - Getting numbers instead of string

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.

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]);

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

你应该用

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM