944,125 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 6177
  • C RSS
Apr 12th, 2007
0

save file names into char array

Expand 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?
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...
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jasssvj is offline Offline
4 posts
since Apr 2007
Apr 12th, 2007
0

Re: save file names into char array

Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
Apr 12th, 2007
0

Re: save file names into char array

>char *filenames[20];

And what happens if you have more than 20 files there?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Apr 12th, 2007
0

Re: save file names into char array

Click to Expand / Collapse  Quote originally posted by iamthwee ...
>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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jasssvj is offline Offline
4 posts
since Apr 2007
Apr 12th, 2007
0

Re: save file names into char array

Click to Expand / Collapse  Quote originally posted by jasssvj ...
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.
Moderator
Reputation Points: 3281
Solved Threads: 895
Posting Sage
WaltP is online now Online
7,748 posts
since May 2006
Apr 13th, 2007
0

Re: save file names into char array

Click to Expand / Collapse  Quote originally posted by WaltP ...
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jasssvj is offline Offline
4 posts
since Apr 2007
Dec 4th, 2007
0

Re: save file names into char array

Click to Expand / Collapse  Quote originally posted by WaltP ...
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...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vonzul is offline Offline
6 posts
since Nov 2007
Dec 4th, 2007
0

Re: save file names into char array

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?
Moderator
Reputation Points: 3281
Solved Threads: 895
Posting Sage
WaltP is online now Online
7,748 posts
since May 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: The Clear
Next Thread in C Forum Timeline: Reading large text file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC