save file names into char array

Reply

Join Date: Apr 2007
Posts: 4
Reputation: jasssvj is an unknown quantity at this point 
Solved Threads: 0
jasssvj jasssvj is offline Offline
Newbie Poster

save file names into char array

 
0
  #1
Apr 12th, 2007
Hi!
I have to make a program, wich lists all files in current dir, and then save all file names into an array.
My program doesn't work ! Can you tell me why?
Thanks a lot
  1. #include <dirent.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <dirent.h>
  6. int main(void)
  7. { FILE *fp;
  8. DIR *d;
  9. struct dirent *dir;
  10. char *filenames[20];
  11. int i=0;
  12. int k,l;
  13. d = opendir(".");
  14. if (d)
  15. {
  16.  
  17. while ((dir = readdir(d)) != NULL)
  18. {
  19.  
  20. filenames[i]=(dir->d_name);
  21. printf("%d",i);
  22. printf(filenames[i]);
  23. printf("\n");
  24. i++;
  25.  
  26. }
  27.  
  28. closedir(d);
  29. }
  30. printf(filenames[3]);
  31.  
  32.  
  33. // *filenames[i]='\0';
  34. system ("pause");
  35. return(0);
Last edited by WaltP; Apr 12th, 2007 at 3:54 pm. Reason: Added CODE tags -- you actually typed right over what they are when you entered this post...
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: save file names into char array

 
0
  #2
Apr 12th, 2007
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: save file names into char array

 
0
  #3
Apr 12th, 2007
>char *filenames[20];

And what happens if you have more than 20 files there?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 4
Reputation: jasssvj is an unknown quantity at this point 
Solved Threads: 0
jasssvj jasssvj is offline Offline
Newbie Poster

Re: save file names into char array

 
0
  #4
Apr 12th, 2007
Originally Posted by iamthwee View Post
>char *filenames[20];

And what happens if you have more than 20 files there?
I don't know how to make it to work for unspecified number of files
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: save file names into char array

 
0
  #5
Apr 12th, 2007
Originally Posted by jasssvj View Post
Hi!
I have to make a program, wich lists all files in current dir, and then save all file names into an array.
My program doesn't work ! Can you tell me why?
You need to tell us why you think it's wrong? We don't know the symptoms you're seeing.

One thing I notice though is you are not saving the file name correctly. Once you read the second file, the first name is gone. You need to move the entire name into your list, not just the pointer. Be sure you redefine filenames so it can hold the string properly.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 4
Reputation: jasssvj is an unknown quantity at this point 
Solved Threads: 0
jasssvj jasssvj is offline Offline
Newbie Poster

Re: save file names into char array

 
0
  #6
Apr 13th, 2007
Originally Posted by WaltP View Post
You need to tell us why you think it's wrong? We don't know the symptoms you're seeing.

One thing I notice though is you are not saving the file name correctly. Once you read the second file, the first name is gone. You need to move the entire name into your list, not just the pointer. Be sure you redefine filenames so it can hold the string properly.
Yes this is the problem,thanks a lot
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: save file names into char array

 
0
  #7
Dec 4th, 2007
Originally Posted by WaltP View Post
One thing I notice though is you are not saving the file name correctly. Once you read the second file, the first name is gone. You need to move the entire name into your list, not just the pointer. Be sure you redefine filenames so it can hold the string properly.

Could you please explain to me how this is done? I wanted to save some file names into an array so I could manipulate the text in the files, but I can't get past this problem and I have tried to save the entire name in the array but I continually get casting messages and segmentation faults. It runs as is, but the array only contains the last file name. I'm using the same program from above...
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: save file names into char array

 
0
  #8
Dec 4th, 2007
Only if you'll bother to read The Rules as requested when you registered, and the post entitled Read Me: Read This Before Posting. Also, please tell us what we could have done to express the importance of reading the rules first?
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC