![]() |
|
#1
|
|||
|
|||
opening a subdirectoryHi,
I am trying to open a series of subdirectory and read the files inside them. so: there is a main directory (called outDirName in the code) which includes a few subdirectories (the number of these subdirectories varies depending on the user choice). Then inside of each subdirectory there are some raw data files that i need to read. In my code, i am trying to open the main directory (outDirName), read its entries and open them one by one. Here is my code: CPP / C++ / C Code:
I may be attacking this problem in the wrong way. I am not even sure if i can use the line: CPP / C++ / C Code:
Please let me know my mistake here. Thank you, Nina |
|
#2
|
||||
|
||||
|
I just gave it a cursory glance but I have been working with the same sort of thing the last few days. Really just some convience classes for dealing with this same stuff under cygwin.
Something that jumps out at me is that you treat ent2 as if you had wanted it to be: CPP / C++ / C Code:
See if that helps you some. Mark __________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work." --Thomas Alva Edison "Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety." --Benjamin Franklin "A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes." --Hugh Downs |
|
#3
|
|||
|
|||
|
Quote:
yes, i have defined ent, ent2, and ent3 as: CPP / C++ / C Code:
thanks, Nina |
|
#4
|
||||
|
||||
|
So, having not run your code, what exactly is the problem? It appears you are opening a dir, finding a dir, and opening that dir. What is your output and what did you expect. I have hardwired in my subdirectories based on a known installation (fltk) but walking the DIR structure and finding directories should be simple considering you got this far.
A good link on the subject is this one. I also got a lot of good info from the book Programming in C by K&R (toward the back of the book). Mark __________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work." --Thomas Alva Edison "Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety." --Benjamin Franklin "A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes." --Hugh Downs |
|
#5
|
|||
|
|||
|
Quote:
When i run the program, it seems that the main directory (named outDirName in the code) opens properly. But the subdirectories inside do not open and the pointer to them returns NULL. CPP / C++ / C Code:
and then i get the following error: Code:
Nina |
|
#6
|
|||
|
|||
|
Quote:
1. When opendir fails, that means that either nothing with that name exists, or that it is a regular file, not a directory. In either case, dir3 is equal to NULL and you should not do things like readdir(dir3). That is, if opendir returns NULL, then just continue the loop, or use an else{} around the remainder of the loop. 2. The following is very bad (and can also cause the program to crash). CPP / C++ / C Code:
You might try something like the following [edit] FileName declaration in the following snippet was bad in the original post [/edit] CPP / C++ / C Code:
Or whatever you need to build up the total path and file name. You should really check string lengths to make sure there is no buffer overflow, and you could certainly make it more efficient, but something like this would work. (A better solution might be to use C++ strings, but either method will work if you do it right.) Regards, Dave [edit] readdir(NULL) can cause the program to crash as well as writing to memory that doesn't belong to the program (strcpy or strcat to pointers that aren't pointing to legal memory for this program). [/edit] Last edited by davekw7x : 06-Jul-2005 at 16:18.
|
|
#7
|
||||
|
||||
|
Quote:
This is the info I was looking for. I got sidetracked looking into stat.h instead of looking at it simply. Quote:
Mark This is my first attempt at drifting from standard usages so I do appreciate the info perhaps as much as the OP. __________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work." --Thomas Alva Edison "Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety." --Benjamin Franklin "A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes." --Hugh Downs |
|
#9
|
|||
|
|||
|
Quote:
it is done on linux platform. I guess there should not be much difference. I appreciate if you could give me suggestions. Nina |
|
#10
|
|||
|
|||
|
Quote:
I check the name of the subdir that opendir is suppose to open. CPP / C++ / C Code:
Quote:
Thanks again, Nina |
Recent GIDBlog
Programming ebook direct download available by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Simple question about file opening | eddo | C++ Forum | 3 | 09-Jun-2005 22:04 |
| not opening file correctly | M3X | C++ Forum | 3 | 16-Apr-2005 11:10 |
| Problem opening .mdb files with CDaoDatabase | shvalb | MS Visual C++ / MFC Forum | 0 | 08-Mar-2005 06:07 |
| opening files and displaying text | pin215 | C++ Forum | 7 | 21-Feb-2004 21:27 |
| Opening files with a twist, C++ | calculus87 | C++ Forum | 2 | 26-Sep-2003 14:41 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The