【问题标题】:Different output when executing from cmd and Codeblocks从 cmd 和 Codeblocks 执行时的不同输出
【发布时间】:2015-06-19 07:08:39
【问题描述】:

以下程序在从 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 没有设置二进制文件所在的当前目录。尝试使用GetCurrentDirectorywin32函数,检查当前目录设置在哪里。
  • CodeBlocks设置的当前目录是-D:\anmol\coding\c++\boost\boost1,可能是因为它是项目的主目录。无论如何,既然我在代码中输入了完整路径,那么当前目录是什么又有什么关系呢?
  • 你想用你的程序解决什么问题?当前目录很重要,因为 D:(最后没有斜杠)解析为驱动器 D 上的当前目录。因此,如果“source”包含驱动器 D: 上的当前目录,则等效()返回 true。
  • @durkmurder 为什么要使用 win32 功能? OP 已经在使用 Boost Filesystem boost.org/doc/libs/1_58_0/libs/filesystem/doc/…
  • @SergeRogatch 编辑了问题以提供上下文。

标签: c++ boost cmd codeblocks boost-filesystem


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    只需替换

    equivalent( source, "D:" )
    

    equivalent( source, "D:\\" )
    

    你应该得到预期的结果:D: 之后的斜线(如 Serge Rogatch 所建议的那样)将使字符串指向根目录。

    【讨论】:

    • 这不能回答我的问题。
    猜你喜欢
    • 2020-03-25
    • 2021-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多