0

我有以下 MWE,它返回一个整数:

#include <iostream>
using namespace std;

int main()
{
    int a = 2;
    return a;
}

现在我想通过 Windows 中的命令行 (cmd) 调用这个程序。这是我如何做到这一点的程序:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

int main()
{
    int a = system("c:\test_batch.exe");
    cout << a << endl;
    return 0;
}

但是,这不会返回值 2,而是 0。我不明白这一点,因为我认为 system() 返回了程序的退出代码,在本例中为 2。

4

1 回答 1

1

system 返回一个由命令解释器返回的值,而不是由实际命令返回的值。

你需要做类似的事情

int a = system("c:\test_batch.exe && exit %ERRORLEVEL%");
于 2012-10-31T12:32:59.500 回答