运行外部程序并获得返回的整数
running external program and obtaining returned integer
我有以下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。我不明白这一点,因为我认为系统返回程序的出口代码,在这种情况下为2。
系统返回命令interpeter返回的值,而不是由实际命令。
您需要做
之类的事情int a = system("c:test_batch.exe && exit %ERRORLEVEL%");
相关文章:
- 让bool方法返回其他整数
- C 字符串返回字符串的整数/双精度/长整型值
- 如何使此递归函数从给定的起始位置返回最小的整数?
- 如何优化代码以返回最接近给定整数的数字,但给定列表中不存在?
- 查找存储在二叉搜索树的所有非叶子中的数据总和?(返回整数的独立递归函数
- C++:为什么我的掷骰子函数在掷骰子数量时只返回偶数整数?
- 从 C++ 中定义的异常返回整数
- 递归返回可被给定整数 k 整除的位数
- 类型转换问题:返回为整数而不是浮点/类型
- 分段故障在类之间返回整数
- 如何将字符串传递给函数并返回整数
- atof 仅返回整数
- AVX/SSE 回合向下浮动并返回整数向量
- 元函数计算 x^n 并返回整数限制而不溢出(如果不可能)
- 我已经声明了整数并尝试返回整数,但它给了我错误
- C++ 堆栈字符串流函数返回整数与 ASCII
- 是否可以从模板函数返回整数
- 为什么 floor 不返回整数?
- 如何同时在本机 dll 中返回整数和字符 * 变量C++?
- 为什么difftime只返回整数