Different results when executed from cmd and Codeblocks

The following program gives different results when executed from CodeBlocks and from cmd -:

#include <iostream> #include <string> #define BOOST_FILESYSTEM_NO_DEPRECATED #include <boost/filesystem.hpp> using namespace std; using namespace boost::filesystem; int main() { // A valid existing folder path on my system. // This is actually the path containing the program exe. path source = "D:\\anmol\\coding\\c++\\boost\\boost1\\bin\\release"; cout << "output = " << equivalent( source, "D:" ) << " !!!\n"; return 0; } 

Exiting CodeBlocks after starting from within the IDE:

 output = 0 !!! 

The result from cmd by executing boost1 after changing the current directory to the folder containing the executable file (the source path specified in the code) is:

 output = 1 !!! 

For me, the result set by CodeBlocks should be correct.

I run this program on the 64-bit version of Windows 7 SP1 and CodeBlocks 13.12.
I am using TDM-GCC 4.9.2 (32-bit) and Boost 1.57 to create this program.

Wrong output from cmd appears only if I run the program after changing the current directory to the folder containing the executable file.
If I save the current cmd directory in a different folder, the correct output is displayed.

EDIT -:

The original problem I was trying to solve was to check if the file / directory was a descendant of another directory or not.
To do this, I executed the following code:

 #include <iostream> #include <string> #define BOOST_FILESYSTEM_NO_DEPRECATED #include <boost/filesystem.hpp> using namespace std; using namespace boost::filesystem; // Returns the difference in height in the filesystem tree, between the directory "parent" and the file/folder "descendant" static int HeightDiff( const path parent, path descendant ) { int diff = 0; while ( !equivalent( descendant, parent ) ) { descendant = descendant.parent_path(); if ( descendant.empty() ) { diff = -1; // "descendant" is not a descendant of "parent" break; } diff++; } return diff; } // Returns true if the file/folder "descendant" is a descendant of the directory "parent" static bool IsDescendant( const path parent, path descendant ) { return HeightDiff( parent, descendant ) >= 1; } int main( int argc, char** argv ) { if ( isDescendant( canonical( argv[1] ), canonical( argv[2] ) ) ) { cerr << "The destination path cannot be a descendant of the source path!! Please provide an alternate destination path !!" << endl; } } 

Now, if I executed the code with argv[1]="D:\anmol\coding\c++\boost\boost1\bin\release" and argv[2]="D:\anmol\coding\c++\boost\boost1\bin" , it will return true when it should return false . (Since in this case parent is actually a descendant descendant )

The reason for this is that during the while loop in HeightDiff() after some iterations, the descendant takes on the value D: Therefore, equivalent() will return true and stop the loop one step before the descendant becomes an empty string.

I did not know before that D: refers to the current directory and therefore asks this question.

Is there a way to change the HeightDiff function so that it HeightDiff correct result?

Will replacing the equivalent() condition with while(descendant != parent) give the correct output in all cases?

If not, is there another solution?

+5
source share
2 answers

After replacing the equivalent condition with while(descendant != parent) program runs correctly.

+2
source

Just replace

 equivalent( source, "D:" ) 

with

 equivalent( source, "D:\\" ) 

and you should get the expected results: the slash after D: (at the suggestion of Sergey Rogach) will force the string to refer to the root directory.

0
source

All Articles