GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-Jul-2005, 14:09
Dream86 Dream86 is offline
Junior Member
 
Join Date: Jun 2004
Posts: 54
Dream86 is on a distinguished road

opening a subdirectory


Hi,

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:

#include <dirent.h>    
#include <string.h>   
#include <sys/stat.h>
#include <stdio.h>
#include <time.h>
#include <iostream>
#include <fstream>
#include <stdlib.h>     
#include "hdr_s.h"     
#include "Format.h"
                       
using namespace std;

int main()
{

  DIR* dir2 = opendir((char*)outDirName); //open main directory
  if ( dir2 == NULL ) 
  {
    cerr << endl << "Unable to open the main input directory." <<     
    endl;
    cerr << "Please try again." << endl << endl;
  }
  DIR* dir3;  //**Should i initialize this pointer to something?
  while ((ent2 = readdir(dir2)) != NULL)
  {
    if((strcmp((char*)ent2->d_name,".")!= 0)&&(strcmp((char*)ent2->d_name,"..")!= 0))
    {
      dir3 = opendir((char*)ent2->d_name);

     //The following message prints out--so it seems that it is unable to open
     //open the sundirs

      if ( dir3 == NULL ) //dir is the pointer to the opened dir
      {
        cerr << endl << "Unable to open the field directory directory." << endl;
        cerr << "Please try again." << endl << endl;
      }
  
      while ((ent3 = readdir(dir3)) != NULL)
      {

        if((strcmp((char*)ent3->d_name,".")!= 0)&&(strcmp((char*)ent3->d_name,"..")!= 0))
        {
          char* fileName = ent3->d_name;
          char* destFile =  strcat((char*)dirNameOne, "/");  //dirNameOne=outDirName
          char* FileName = strcat((char*)destFile, (char*)fileName);
          ifstream inFile((char*)FileName, ios::in|ios::binary);
          cout << "name of the first file in the directory: " << FileName<< endl;
          if (inFile.fail())
          {
             cout << "***File was not opened properly for reading the header***"   << endl;
             break;
          }
          else
          {

          // ...

           }

  return 0;
}

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:
dir3 = opendir((char*)ent2->d_name);

Please let me know my mistake here.

Thank you,

Nina
  #2  
Old 06-Jul-2005, 14:31
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Florina, Greece
Posts: 1,112
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
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:
dirent* ent2;

See if that helps you some.

Mark
__________________

"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

Stories from the NICU Blog
  #3  
Old 06-Jul-2005, 14:37
Dream86 Dream86 is offline
Junior Member
 
Join Date: Jun 2004
Posts: 54
Dream86 is on a distinguished road
Quote:
Something that jumps out at me is that you treat ent2 as if you had wanted it to be:
CPP / C++ / C Code:
dirent* ent2;

See if that helps you some.

Mark

yes, i have defined ent, ent2, and ent3 as:
CPP / C++ / C Code:
struct dirent* ent;
struct dirent* ent2;
struct dirent* ent3;

thanks,

Nina
  #4  
Old 06-Jul-2005, 14:47
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Florina, Greece
Posts: 1,112
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
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
__________________

"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

Stories from the NICU Blog
  #5  
Old 06-Jul-2005, 15:05
Dream86 Dream86 is offline
Junior Member
 
Join Date: Jun 2004
Posts: 54
Dream86 is on a distinguished road
Quote:
Originally Posted by cable_guy_67
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 http://www.devx.com/cplus/10MinuteSolution/26748/0/page/2. I also got a lot of good info from the book Programming in C by K&R (toward the back of the book).

Mark

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:
if ( dir3 == NULL ) 
{
  cerr << endl << "Unable to open the field directory directory." << endl;
  cerr << "Please try again." << endl << endl;
}
//dir3 is the pointer to the subdirectories and the above message prints out
//so i guess dir3 is not really pointing to the sundirs inside outDirName

and then i get the following error:
Code:
Memory fault (core dumped)

Nina
  #6  
Old 06-Jul-2005, 15:45
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,153
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by Dream86
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:
if ( dir3 == NULL ) 
{
  cerr << endl << "Unable to open the field directory directory." << endl;
  cerr << "Please try again." << endl << endl;
}
//dir3 is the pointer to the subdirectories and the above message prints out
//so i guess dir3 is not really pointing to the sundirs inside outDirName

and then i get the following error:
Code:
Memory fault (core dumped)

Nina

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:
char* fileName = ent3->d_name;
          char* destFile =  strcat((char*)dirNameOne, "/");  //dirNameOne=outDirName
          char* FileName = strcat((char*)destFile, (char*)fileName);

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:
char FileName[1024];
strcpy(FileName, dirNameOne);
strcat(FileName, "/");
strcat(FileName, destFile);

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  
Old 06-Jul-2005, 16:01
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Florina, Greece
Posts: 1,112
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
Quote:
Originally Posted by davekw7x
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.

This is the info I was looking for. I got sidetracked looking into stat.h instead of looking at it simply.

Quote:
Originally Posted by opengroup.org/onlinepubs
RETURN VALUE

Upon successful completion, opendir() returns a pointer to an object of type DIR. Otherwise, a null pointer is returned and errno is set to indicate the error.

Mark

This is my first attempt at drifting from standard usages so I do appreciate the info perhaps as much as the OP.
__________________

"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

Stories from the NICU Blog
  #8  
Old 07-Jul-2005, 07:54
Kacyndra's Avatar
Kacyndra Kacyndra is offline
Member
 
Join Date: May 2005
Location: Maryland
Posts: 233
Kacyndra will become famous soon enough
is this done in unix?
becase i wrote the same program for a class on unix platform, i can look it up if you want?
__________________
Xrum!
  #9  
Old 07-Jul-2005, 08:42
Dream86 Dream86 is offline
Junior Member
 
Join Date: Jun 2004
Posts: 54
Dream86 is on a distinguished road
Quote:
Originally Posted by Kacyndra
is this done in unix?
becase i wrote the same program for a class on unix platform, i can look it up if you want?

it is done on linux platform. I guess there should not be much difference. I appreciate if you could give me suggestions.

Nina
  #10  
Old 07-Jul-2005, 08:50
Dream86 Dream86 is offline
Junior Member
 
Join Date: Jun 2004
Posts: 54
Dream86 is on a distinguished road
Quote:
Originally Posted by davekw7x
1.
When opendir fails, that means that either nothing with that name exists, or that it is a regular file, not a directory.

I check the name of the subdir that opendir is suppose to open.
CPP / C++ / C Code:
cout <<  ent2->d_name ; //this prints out the correct name
dir3 = opendir((char*)ent2->d_name);
but may be i need to give it the complete path of the subdir and not only its name by ent2->d_name. I do not think this gives the complete path of the subdir to opendir. I will to give the complete path of the subdir to opendir(), and let you know the results.

Quote:
Originally Posted by davekw7x
2. The following is very bad (and can also cause the program to crash).

CPP / C++ / C Code:
char* fileName = ent3->d_name;
char* destFile =  strcat((char*)dirNameOne, "/"); 

You might try something like the following
CPP / C++ / C Code:
strcpy(FileName, dirNameOne);
strcat(FileName, "/");
strcat(FileName, destFile);

Thank you. I made the changes as you said.

Thanks again,

Nina
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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 · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:10.


vBulletin, Copyright © 2000 - 2014, Jelsoft Enterprises Ltd.