| | |
save file names into char array
![]() |
•
•
Join Date: Apr 2007
Posts: 4
Reputation:
Solved Threads: 0
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
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
c Syntax (Toggle Plain Text)
#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[20]; int i=0; int k,l; d = opendir("."); if (d) { while ((dir = readdir(d)) != NULL) { filenames[i]=(dir->d_name); printf("%d",i); printf(filenames[i]); printf("\n"); i++; } closedir(d); } printf(filenames[3]); // *filenames[i]='\0'; system ("pause"); 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...
•
•
•
•
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?
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Apr 2007
Posts: 4
Reputation:
Solved Threads: 0
•
•
•
•
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.
•
•
Join Date: Nov 2007
Posts: 6
Reputation:
Solved Threads: 0
•
•
•
•
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...
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
- RE: reading .wav file and putting ito inot the array (C)
- understanding file system (C++)
- Windows programming - C - Save file function (C++)
- reading txt file into array (C++)
Other Threads in the C Forum
- Previous Thread: The Clear
- Next Thread: Reading large text file
| Thread Tools | Search this Thread |
#include * adobe ansi array arrays asterisks binarysearch centimeter changingto char character cm convert copyimagefile cprogramme creafecopyofanytypeoffileinc createprocess() database dynamic execv feet fgets file floatingpointvalidation fork function getlogicaldrivestrin givemetehcodez grade gtkwinlinux hacking histogram ide inches include incrementoperators infiniteloop input interest intmain() iso kernel keyboard kilometer km license linked linkedlist linux locate looping lowest matrix meter microsoft number oddnumber open opendocumentformat openwebfoundation owf pattern pdf performance pointer posix power probleminc process program programming radix recursion recv recvblocked research reversing segmentationfault sequential single socket socketprograming socketprogramming standard strchr string suggestions systemcall test threads turboc unix urboc user variable voidmain() wab whythiscodecausesegmentationfault windowsapi






