Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different output when executing 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's exe.
    path source = "D:\\anmol\\coding\\c++\\boost\\boost1\\bin\\release";

    cout << "output =  " << equivalent( source, "D:" ) << " !!!\n";
    return 0;
}

The output from CodeBlocks after running it from inside the IDE -:

output = 0 !!!

The output from cmd by executing boost1 after changing the current directory to the folder containing the executable ( the source path mentioned in the code ) -:

output = 1 !!!

According to me, the output given by CodeBlocks should be the correct one.

I am running this program on Windows 7 SP1 64-bit and CodeBlocks 13.12.
I am using TDM-GCC 4.9.2 (32-bit) and Boost 1.57 to build this program.

The wrong output from cmd comes only if I execute the program after changing the current directory to the folder containing the executable.
If I keep the cmd's current directory to some other folder, the correct output is displayed.

EDIT -:

The original problem I was trying to solve was to check whether a file/directory was a descendant of another directory or not.
For accomplishing that, I implemented 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 would return true, when it should have returned false instead. (Since, in this case, the parent is actually the descendant of descendant)

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

I did not know earlier that D: refers to the current directory, and hence asked this question.

Is there any way to modify the HeightDiff function so that it gives the correct output ?

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

If not, then is there any other solution ?

like image 400
Anmol Singh Jaggi Avatar asked Oct 31 '22 01:10

Anmol Singh Jaggi


1 Answers

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

like image 178
Anmol Singh Jaggi Avatar answered Nov 04 '22 07:11

Anmol Singh Jaggi