从 cmd 和代码块执行时输出不同
Different output when executing from cmd and Codeblocks
以下程序在从 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 内部运行代码块后的输出 -:
output = 0 !!!
cmd 的输出,在将当前目录更改为包含可执行文件的文件夹(代码中提到的source
路径)后执行boost1
-:
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:anmolcodingc++boostboost1binrelease"
和 argv[2]="D:anmolcodingc++boostboost1bin"
执行代码,它将返回 true
,而它应该返回 false
代替。(因为在这种情况下,parent
实际上是descendant
的后代)
原因是在 HeightDiff()
的 while 循环中,经过一些迭代,descendant
会将值取D:
。因此,equivalent()
将返回 true 并在循环变为空字符串之前一步停止循环descendant
。
我之前并不知道D:
指的是当前目录,因此问了这个问题。
有没有办法修改HeightDiff
函数,以便它给出正确的输出?
将equivalent()
条件替换为while(descendant != parent)
在所有情况下都能提供正确的输出吗?
如果没有,那么还有其他解决方案吗?
将equivalent
条件替换为 while(descendant != parent)
后,程序工作正常。
只需替换
equivalent( source, "D:" )
跟
equivalent( source, "D:\" )
你应该得到预期的结果:D:
后面的斜杠(如Serge Rogatch所建议的)将使字符串引用根目录。
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 无法在问题解决方案中执行输出逻辑
- 如何在程序执行时查看Valgrind Massif输出(或其他堆分析器)?
- 如何将图像传入C++可执行文件并将输出图像存储在新目录中?
- 执行此代码时,它不显示任何输出.为什么?
- 如何在C++中执行命令并获取命令的返回代码标准输出和标准
- 在程序执行期间从标准输出重定向到自定义流
- CMake将依赖项复制到可执行输出路径
- 执行ls命令并打印输出
- 为什么当我正确执行语法时,我的代码输出为 0
- 为什么 C++ 中的 THREADS 在执行时给出不同的输出?
- c++:if 语句无法正常运行,并继续执行无效输出
- 将我的主输出库与测试可执行文件链接时出现问题
- 获取生成文件以更改输出可执行文件名称
- popen() 将执行命令的输出写入 cout
- 如何从控制台启动可执行文件并使用Qt读取输出
- 在 Windows 命令行中运行.exe在使用 2 种相同方式执行时会产生不同的输出
- 在标准输出中执行打印会导致数组中随机分配值
- 来自不同位置的数组输出的执行时间
- 执行CMAKE目标的输出作为另一个目标的依赖