Saving file names from directory to array

Thread Solved
Reply

Join Date: Nov 2007
Posts: 6
Reputation: vonzul is an unknown quantity at this point 
Solved Threads: 0
vonzul vonzul is offline Offline
Newbie Poster

Saving file names from directory to array

 
0
  #1
Dec 4th, 2007
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.

  1. #include <dirent.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <dirent.h>
  6.  
  7.  
  8. int main(void)
  9. { FILE *fp;
  10. DIR *d;
  11. struct dirent *dir;
  12. char *filenames[8];
  13. //char *filenames = malloc(sizeof *filenames);
  14. int i=0;
  15. int a;
  16. d = opendir("revisions");
  17. if (d)
  18. {
  19. while ((dir = readdir(d)) != NULL)
  20. {
  21. //strcpy((char*)(long)filenames[i],(void *)(dir->d_name));
  22. //*(char**)&filenames[i]=gets(dir->d_name);
  23. filenames[i]=(dir->d_name);
  24. printf("%s",filenames[i]);
  25. //printf("%s",*(char**)&filenames[i]);
  26. printf("\n");
  27. i++;
  28.  
  29. }
  30. printf("\n");
  31. closedir(d);
  32. }
  33. for (a = 2; a < i; a++){
  34. //printf(*(char**)&filenames[a]);
  35. printf("%s",filenames[a]);
  36. printf("\n");
  37. }
  38.  
  39. return(0);
  40. }


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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,151
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1435
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Saving file names from directory to array

 
0
  #2
Dec 4th, 2007
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.
Last edited by Ancient Dragon; Dec 4th, 2007 at 4:47 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 6
Reputation: vonzul is an unknown quantity at this point 
Solved Threads: 0
vonzul vonzul is offline Offline
Newbie Poster

Re: Saving file names from directory to array

 
0
  #3
Dec 4th, 2007
Originally Posted by Ancient Dragon View Post
line 23 needs to call malloc() to allocate memory for the name then call strcpy() to copy the name.

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

and I changed line 24 to
  1. 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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,151
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1435
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Saving file names from directory to array

 
0
  #4
Dec 4th, 2007
>>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.

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

line 33: >> for (a = 2; a < i; a++){
why is that loop starting with 2 instead of 0.
Last edited by Ancient Dragon; Dec 4th, 2007 at 5:48 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 6
Reputation: vonzul is an unknown quantity at this point 
Solved Threads: 0
vonzul vonzul is offline Offline
Newbie Poster

Re: Saving file names from directory to array

 
0
  #5
Dec 4th, 2007
Originally Posted by Ancient Dragon View Post
And, in C language, you do not have to typecase the return value of malloc. Required in c++, but not in C.

  1. filenames[ i ] = malloc(strlen(dir->d_name)+1);
  2. 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

>> 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 '..'
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,151
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1435
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Saving file names from directory to array

 
0
  #6
Dec 4th, 2007
>>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.
Last edited by Ancient Dragon; Dec 4th, 2007 at 10:29 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 6
Reputation: vonzul is an unknown quantity at this point 
Solved Threads: 0
vonzul vonzul is offline Offline
Newbie Poster

Re: Saving file names from directory to array

 
0
  #7
Dec 4th, 2007
Originally Posted by Ancient Dragon View Post
Post code because what I posted is correct.
  1. #include <dirent.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5.  
  6.  
  7. int main(void)
  8. { FILE *fp;
  9. DIR *d;
  10. struct dirent *dir;
  11. char *filenames;
  12. int a;
  13. int i = 0;
  14. d = opendir("revisions");
  15. if (d)
  16. {
  17. while ((dir = readdir(d)) != NULL)
  18. {
  19. filenames[i] = malloc(strlen(dir->d_name)+1);
  20. strcpy(filenames[i],dir->d_name);
  21. printf("%s", filenames);
  22. printf("\n");
  23. i++;
  24.  
  25. }
  26. printf("\n");
  27. closedir(d);
  28. }
  29. for (a = 2; a < 8; a++){
  30. printf("%s", filenames[a]);
  31. printf("\n");
  32. }
  33.  
  34. return(0);
  35. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,151
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1435
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Saving file names from directory to array

 
0
  #8
Dec 5th, 2007
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 6
Reputation: vonzul is an unknown quantity at this point 
Solved Threads: 0
vonzul vonzul is offline Offline
Newbie Poster

Re: Saving file names from directory to array

 
0
  #9
Dec 5th, 2007
Thank you very much! That was the problem!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC