执行shell脚本获取返回值字符串

std::string executeShellCommand(const std::string &command)
{
	FILE* pipe = popen(command.c_str(), "r");
	if (!pipe) return "ERROR";
	char buffer[128];
	std::string result = "";
	while(!feof(pipe))
	{
		if(fgets(buffer, 128, pipe) != nullptr)
			result += buffer;
	}
	pclose(pipe);
	return result;
}
posted @ 2023-06-16 09:29  卡尔的思索  阅读(153)  评论(0编辑  收藏  举报