从命令行和Codeblocks执行时输出不同

5
以下程序在从CodeBlocks和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;
}

在IDE中运行CodeBlocks后的输出结果为:
output = 0 !!!

将当前目录更改为包含可执行文件的文件夹(代码中提到的source路径),并执行boost1后从cmd输出 -:

output = 1 !!!

据我所知,CodeBlocks给出的输出应该是正确的。
我在Windows 7 SP1 64位和CodeBlocks 13.12上运行此程序。
我使用TDM-GCC 4.9.2(32位)和Boost 1.57构建此程序。
只有当我将当前目录更改为包含可执行文件的文件夹后才会从cmd得到错误的输出。
如果我将cmd的当前目录保留在其他文件夹中,则会显示正确的输出。
编辑 -: 我最初尝试解决的问题是检查文件/目录是否是另一个目录的子目录。
为了实现这一点,我实现了以下代码-:
#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;
    }
}

现在,如果我用argv[1]="D:\anmol\coding\c++\boost\boost1\bin\release"argv[2]="D:\anmol\coding\c++\boost\boost1\bin"执行代码,它将返回true,但实际上应该返回false。(因为在这种情况下,parent实际上是descendant的后代)。
原因是在HeightDiff()的while循环中,在一些迭代之后,descendant将取值D:。因此,equivalent()将返回true并停止循环,使descendant成为空字符串之前多了一步。
我之前不知道D:指的是当前目录,因此才会问这个问题。
有没有办法修改HeightDiff函数以获得正确的输出?
while(descendant != parent)替换equivalent()条件是否可以在所有情况下给出正确的输出?
如果不能,那么有其他解决方案吗?

似乎CodeBlocks没有设置二进制文件所在的当前目录。尝试使用GetCurrentDirectory win32函数来检查当前目录的设置位置。 - durkmurder
CodeBlocks设置的当前目录是“D:\ anmol \ coding \ c ++ \ boost \ boost1”,可能是因为这是项目的主目录。无论如何,既然我在我的代码中输入了完整路径,那么当前目录有什么关系呢? - Anmol Singh Jaggi
1
你的程序想要解决什么问题?当前目录很重要,因为D:(末尾没有斜杠)会解析为驱动器D上的当前目录。因此,如果“source”包含驱动器D上的当前目录,则equivalent()函数将返回true。 - Serge Rogatch
1
@durkmurder 为什么要使用win32函数?OP已经在使用Boost文件系统了http://www.boost.org/doc/libs/1_58_0/libs/filesystem/doc/reference.html#current_path - sehe
@SergeRogatch 编辑了问题以提供上下文。 - Anmol Singh Jaggi
2个回答

2

equivalent条件替换为while(descendant != parent)后,程序正常工作。


0

只需替换即可

equivalent( source, "D:" )

使用

equivalent( source, "D:\\" )

你应该得到预期的结果:在D:后面加上斜杠(如Serge Rogatch所建议的)将使字符串引用根目录。


这并没有回答我的问题。 - Anmol Singh Jaggi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接