Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何使用Qt检查路径中是否存在程序?_C++_Qt_Command_Qprocess - Fatal编程技术网

C++ 如何使用Qt检查路径中是否存在程序?

C++ 如何使用Qt检查路径中是否存在程序?,c++,qt,command,qprocess,C++,Qt,Command,Qprocess,我有以下代码: QProcess* proceso = new QProcess(); QString programa = "unknow -v"; proceso->start(programa); proceso->waitForFinished(); QString normal = proceso->readAllStandardOutput(); QString errores = proceso->readAllStandardError(); qDeb

我有以下代码:

QProcess* proceso = new QProcess();
QString programa = "unknow -v";
proceso->start(programa);
proceso->waitForFinished();

QString normal = proceso->readAllStandardOutput();
QString errores = proceso->readAllStandardError();

qDebug() << normal;
qDebug() << errores;
QProcess*proceso=new QProcess();
QString programa=“未知-v”;
proceso->启动(程序A);
proceso->waitForFinished();
QString normal=proceso->readAllStandardOutput();
QString errors=proceso->readAllStandardError();

qDebug()您可以使用
getenv(“PATH”)
获取文件的值,然后在冒号(或Windows的分号)上拆分它,在每个目录上迭代,测试它是否包含未知的
文件,等等

所以你不需要任何Qt的东西。普通的C++(操作).< (这并不是防弹的:其他一些进程可能会在这样的测试和进程的实际开始之间修改
$PATH
中的目录;但在实践中这应该足够了)

在POSIX系统上,您可以通过
sh-c
运行命令(例如,运行
sh-c'unknow-v'
),但要小心转义和(因此请检查字符串
unknow-v
中的单引号和双引号等。)

您也可以使用,但我不建议这样做(太复杂)

无论如何,我不确定这是否值得麻烦。你为什么不直接运行这个程序。。。。我看不出丢失的可执行文件与由于许多其他原因失败的命令之间有多大区别。

请尝试以下操作:

QProcess program;
QString commandToStart= "unknown -v";
QStringList environment = program.systemEnvironment();
program.start(commandToStart);
bool started = program.waitForStarted();
if (!program.waitForFinished(10000)) // 10 Second timeout
    program.kill();

int exitCode = program.exitCode();
QString stdOutput = QString::fromLocal8Bit(program.readAllStandardOutput());
QString stdError = QString::fromLocal8Bit(program.readAllStandardError());
如果
started
true
,则可以启动程序。这通常意味着,它在道路上。如果
false
,请检查
环境中的路径是否正确


只有当流程实际上可以启动并且出现其他问题时,
exitCode
才有帮助。如果程序根本无法启动,
exitCode
将为
0
stdOutput
stdError
将为空!这可能会产生误导。

问题是如果系统上存在命令,您是否能够运行该命令。这意味着不要尝试启动它,看看会发生什么,巨大的不同

这个怎么样

我喜欢
的想法,但它在Windows下不起作用

    QProcess findProcess;
    QStringList arguments;
    arguments << myCommand;
    findProcess.start("which", arguments);
    findProcess.setReadChannel(QProcess::ProcessChannel::StandardOutput);

    if(!findProcess.waitForFinished())
        return false; // Not found or which does not work

    QString retStr(findProcess.readAll());

    retStr = retStr.trimmed();

    QFile file(retStr);
    QFileInfo check_file(file);
    if (check_file.exists() && check_file.isFile())
        return true; // Found!
    else
        return false; // Not found!
QProcess findProcess;
QStringList参数;

参数什么是
unknow-v
,是用户输入的字符串吗?@BasileStarynkevitch谢谢,
unknow
是一个应该在路径中的程序。
    QProcess findProcess;
    QStringList arguments;
    arguments << myCommand;
    findProcess.start("which", arguments);
    findProcess.setReadChannel(QProcess::ProcessChannel::StandardOutput);

    if(!findProcess.waitForFinished())
        return false; // Not found or which does not work

    QString retStr(findProcess.readAll());

    retStr = retStr.trimmed();

    QFile file(retStr);
    QFileInfo check_file(file);
    if (check_file.exists() && check_file.isFile())
        return true; // Found!
    else
        return false; // Not found!