DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C (http://www.daniweb.com/forums/forum118.html)
-   -   Saving file names from directory to array (http://www.daniweb.com/forums/thread99538.html)

vonzul Dec 4th, 2007 3:12 am
Saving file names from directory to array
 
This program should traverse a directory and save the file names into an array so I can manipulate data within the files. The problem is that the program prints the name of the files in the first loop, but it only saves the last file name in the array. Below is a copy of the code...The commented lines are other things that I have tried, but I get the same output. This is a small part of many programs I must write for my research, so I know the name of the directory and how many files are in the directory.

#include <dirent.h>  
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>


int main(void)
{ FILE *fp;
  DIR *d;
  struct dirent *dir;
  char *filenames[8];
  //char *filenames = malloc(sizeof *filenames);
  int i=0;
  int a;
  d = opendir("revisions");
  if (d)
  {
    while ((dir = readdir(d)) != NULL)
    {
      //strcpy((char*)(long)filenames[i],(void *)(dir->d_name));
      //*(char**)&filenames[i]=gets(dir->d_name);
      filenames[i]=(dir->d_name);
      printf("%s",filenames[i]);
      //printf("%s",*(char**)&filenames[i]);
      printf("\n");
      i++;
     
    }
    printf("\n");
    closedir(d);
  }
  for (a = 2; a < i; a++){
    //printf(*(char**)&filenames[a]);
    printf("%s",filenames[a]);
    printf("\n");
  }

  return(0);
}


The output is as follows:
.
..
v1
v2
v3
v4
v5
v6

v6
v6
v6
v6
v6
v6

I was expecting:

.
..
v1
v2
v3
v4
v5
v6

v1
v2
v3
v4
v5
v6

Ancient Dragon Dec 4th, 2007 4:45 am
Re: Saving file names from directory to array
 
line 23 needs to call malloc() to allocate memory for the name then call strcpy() to copy the name. Hardcoding the number of filename pointers in your program is ok only if you know in advance the maximum you will need. But what happens to your program if there are 10 filenames in that directory? Or 100 filenames ? In those cases you will want to use a linked list of names.

vonzul Dec 4th, 2007 4:19 pm
Re: Saving file names from directory to array
 
Quote:

Originally Posted by Ancient Dragon (Post 483669)
line 23 needs to call malloc() to allocate memory for the name then call strcpy() to copy the name.


I changed line 23 to
filenames = (char *) malloc(sizeof *filenames);

and I changed line 24 to
strcpy(filenames,dir->d_name);

I know that this solves the problem I was having because when I change the printf lines to "%d" instead of "%s" this is the output I get:

46
46
115
116
49
46
116
120

115
116
49
46
116
120

But when I change the printf lines back to "%s", this is the output I end up with:

.
..
v1
v2
v3
v4
v5
v6

10 [main] a 1312 _cygtls::handle_exceptions: Error while dumping state (probably corrupted stack)
Segmentation faults(core dumped)

Why is this?

Ancient Dragon Dec 4th, 2007 5:44 pm
Re: Saving file names from directory to array
 
>>filenames = (char *) malloc(sizeof *filenames);

That solves nothing. what is sizeof(*filenames) ?? Answer: number of bytes allocated to the array, not the number of bytes needed to copy d_name into the array. Here is what it should be: Note that you have to use the i counter because filenames is an array of pointers. And, in C language, you do not have to typecase the return value of malloc. Required in c++, but not in C.

filenames[ i ] =  malloc(strlen(dir->d_name)+1);
strcpy(filenames[i],dir->d_name);

line 33: >> for (a = 2; a < i; a++){
why is that loop starting with 2 instead of 0.

vonzul Dec 4th, 2007 9:13 pm
Re: Saving file names from directory to array
 
Quote:

Originally Posted by Ancient Dragon (Post 484127)
And, in C language, you do not have to typecase the return value of malloc. Required in c++, but not in C.

filenames[ i ] =  malloc(strlen(dir->d_name)+1);
strcpy(filenames[i],dir->d_name);

When I tried this, I got two warnings:
1. assignment makes integer from pointer without cast
2. passing arg 1 of 'strcpy' makes pointer from integer without cast

Quote:

>> for (a = 2; a < i; a++){
why is that loop starting with 2 instead of 0.
I have it starting at 2 because I only need the filenames, not the '.' and the '..'

Ancient Dragon Dec 4th, 2007 10:28 pm
Re: Saving file names from directory to array
 
>>When I tried this, I got two warnings:
>>1. assignment makes integer from pointer without cast
>>2. passing arg 1 of 'strcpy' makes pointer from integer without cast

Post code because what I posted is correct.

>>I have it starting at 2 because I only need the filenames, not the '.' and the '..'
Oh I see. You are correct. Normally we would not put them in the list.

vonzul Dec 4th, 2007 11:27 pm
Re: Saving file names from directory to array
 
Quote:

Originally Posted by Ancient Dragon (Post 484279)
Post code because what I posted is correct.

#include <dirent.h>  
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


int main(void)
{ FILE *fp;
  DIR          *d;
  struct dirent *dir;
  char *filenames;
  int a;
  int i = 0;
  d = opendir("revisions");
  if (d)
  {
    while ((dir = readdir(d)) != NULL)
    {
      filenames[i] = malloc(strlen(dir->d_name)+1);
      strcpy(filenames[i],dir->d_name);
      printf("%s", filenames);
      printf("\n");
      i++;

    }
    printf("\n");
    closedir(d);
  }
  for (a = 2; a < 8; a++){
    printf("%s", filenames[a]);
    printf("\n");
  }

  return(0);
}

Ancient Dragon Dec 5th, 2007 12:12 am
Re: Saving file names from directory to array
 
See line 11: you changed the declaration of filenames from an array of pointers to a simple pointer that is no longer an array. Change it back the way it was in your original post. You also have to change line 21 to use the array index.

vonzul Dec 5th, 2007 2:12 am
Re: Saving file names from directory to array
 
Thank you very much! That was the problem!


All times are GMT -4. The time now is 6:49 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC