c++ - 从 cmd 和 Codeblocks 执行时输出不同

标签 c++ boost cmd codeblocks boost-filesystem

以下程序在从 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 函数以使其提供正确的输出吗?

equivalent() 条件替换为 while(descendant != parent) 会在所有情况下给出正确的输出吗?

如果不行,有没有其他解决办法?

最佳答案

while(descendant != parent) 替换 equivalent 条件后,程序运行正常。

关于c++ - 从 cmd 和 Codeblocks 执行时输出不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30932049/

相关文章:

C++ 条件运算符性能

c++ - boost::asio io_service 在 stop() 后不返回

c++ - 什么是 boost::variant 内存和性能成本?

windows - 使用命令提示符查找 CPU 数和每个 CPU 的内核数

c++ - winapi c++ 窗口不少于

c++ - 内存碎片整理在 64 位系统上是否仍然相关

variables - : %%a and %variable% variables? 有什么区别

c# - 通过命令行传递 JSON 字符串

c++ - 使用矩阵、欧拉角和/或四元数进行旋转表示的优缺点是什么?

logging - Boost 1.52.0 上的 Boost.Log 安装错误