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
  #11  
Old 07-Jul-2005, 14:08
Dream86 Dream86 is offline
Junior Member
 
Join Date: Jun 2004
Posts: 54
Dream86 is on a distinguished road
okay, i think one problem was that inorder to open a subdirectory, the complete path of it should be given to opendir().
so i modified the code as follows:
CPP / C++ / C Code:
  DIR* dir2 = opendir((char*)outDirName); //open main output directory
  if ( dir2 == NULL ) 
  {
    cerr << endl << "Unable to open the main input directory directory." << endl;
    cerr << "Please try again." << endl << endl;
  }
  DIR* dir3 = NULL;
  while ((ent2 = readdir(dir2)) != NULL)
  {
  	if((strcmp((char*)ent2->d_name,".")!= 0)&&(strcmp((char*)ent2->d_name,"..")!= 0))
    {
    	char subdirName[2048];
    	char outDirName8[3072];
    	strcpy(outDirName8, outDirName);
    	char path[1024];
    	strcpy(path, "/usr/home/nina/Lab/Codes/inProgress2/");  //here i am giving it a set path 
    	strcat((char*)path, (char*)outDirName8);
    	strcat((char*)path, "/");
    	strcpy(subdirName, ent2->d_name);
    	strcat((char*)path, (char*)subdirName);
    
    	//in the following i am trying to read the files in the subdirs
       //but it does not work and the files do not open properly
  		dir3 = opendir((char*)path);
  		if ( dir3 == NULL ) 
      {
    		cerr << endl << "Unable to open the field directory directory." << endl;
    		cerr << "Please try again." << endl << endl;
  		}
  
  		while ((ent3 = readdir(dir3)) != NULL)
  		{
    		if((strcmp((char*)ent->d_name,".")!= 0)&&(strcmp((char*)ent->d_name,"..")!= 0))
    		{
      		char* fileName = ent->d_name;
      		strcat((char*)path, "/");
      		strcat((char*)path, (char*)ent->d_name);
      		char destFile[4048];
      	  strcpy(destFile, dirNameOne);
      		strcat((char*)destFile, "/");
      		strcat((char*)destFile, (char*)subdirName);
      		strcat((char*)destFile, "/");
      		strcat((char*)destFile, (char*)fileName);
      		ifstream inFile((char*)destFile, ios::in|ios::binary);
	  		  if (inFile.fail())
	  			{
	    			cout << "***File was not opened properly for reading the header***" << endl;
	  				break;
					}


so first
1) Is there a way that my program would find the current compelte path?

2) But now after the subdir is opened, the files does not open.

can anyone give me suggestions on how to attack the above two issues?

Thank you,

Nina
  #12  
Old 07-Jul-2005, 14:48
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
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 behold
Quote:
Originally Posted by Dream86
de as follows:
CPP / C++ / C Code:
 
    	//in the following i am trying to read the files in the subdirs
 
      		char* fileName = ent->d_name;
      		strcat((char*)path, "/");
      		strcat((char*)path, (char*)ent->d_name);
      		char destFile[4048];
      	  strcpy(destFile, dirNameOne);
      		strcat((char*)destFile, "/");
      		strcat((char*)destFile, (char*)subdirName);
      		strcat((char*)destFile, "/");
      		strcat((char*)destFile, (char*)fileName);
      		ifstream inFile((char*)destFile, ios::in|ios::binary);
	

so first
1) Is there a way that my program would find the current compelte path?

2) But now after the subdir is opened, the files does not open.

can anyone give me suggestions on how to attack the above two issues?

Thank you,

Nina

1. You give it the path any way that you want it to. The program can't automatically find anything. How would it know? There is no operating system-independent method for a program to discover unambiguously the absolute path to itself, I think.

You can use a command line argument or you can hard code it directly into the program. Or you can use a path relative to the location of the executable file. If the directories are subdirectories of place where the executable code resides, you can use "." as the top directory name.

2. I think you have the right idea. You need to see what the program is seeing when it tries to open a file. So you can do something like this:

CPP / C++ / C Code:
   	//in the following i am trying to read the files in the subdirs
 
    char* fileName = ent->d_name;
    strcat(path, "/");
    strcat(path, (char*)ent->d_name);
    char destFile[4048];
    strcpy(destFile, dirNameOne);
    strcat(destFile, "/");
    strcat(destFile, (char*)subdirName);
    strcat(destFile, "/");
    strcat(destFile, (char*)fileName);
    cout << "Attempting to open <" << destFile << ">" << endl;
    ifstream inFile(destFile, ios::in|ios::binary);
    if (inFile.fail()) {
      cout << "Couldn't open file." << endl;
      inFile.clear(); //some compiler code doesn't clear this automatically
    }
    else {
      cout << "File was opened successfully." << endl;
      inFile.close();
        //  or do whatever you want to do
    }


Get the idea? Once the program is up and almost running, make the program itself tell you what's happening. You can use this to see what's wrong (or to ask questions that someone else might be able to help you answer).

Regards,

Dave
  #13  
Old 07-Jul-2005, 16:21
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by davekw7x
Quote:
Originally Posted by Dream86
1) Is there a way that my program would find the current compelte path?
1. You give it the path any way that you want it to. The program can't automatically find anything. How would it know?
Not exactly. The program needs to find the path the program was invoked in (not from). The program has access to the current directory.

Quote:
Originally Posted by davekw7x
There is no operating system-independent method for a program to discover unambiguously the absolute path to itself, I think.
This is true. It depends on the hooks into the OS. But each compiler/OS generally has functions to query this info from the OS. In the case of Borland (and I think MS) C/C++ the command is getcwd() (get current working directory). LINUX I'm sure has a similar function.

And in fact it does -- here. You just had to know what to look for...
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

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 23:04
not opening file correctly M3X C++ Forum 3 16-Apr-2005 12:10
Problem opening .mdb files with CDaoDatabase shvalb MS Visual C++ / MFC Forum 0 08-Mar-2005 07:07
opening files and displaying text pin215 C++ Forum 7 21-Feb-2004 22:27
Opening files with a twist, C++ calculus87 C++ Forum 2 26-Sep-2003 15:41

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 09:23.


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