php shell_exec的c ++等价物

时间:2013-02-28 13:36:39

标签: php c++ shell-exec

下面提到的php命令的C ++等效命令是什么:

$command = shell_exec("sqlldr {$connect_string} control={$ctl_file_name} log={$log_file_name}");

2 个答案:

答案 0 :(得分:0)

尝试forkpty,您将获得一个文件描述符,您可以使用该描述符从其他伪终端读取。

答案 1 :(得分:0)

因此,基于您的评论,一个可行的解决方案是使用popen(3):

#include <cstdio>
#include <iostream>
#include <string>

int main()
{
   // Set file names based on your input etc... just using dummies below
   std::string
     ctrlFileName = "file1",
     logFileName  = "file2",
     cmd = "sqlldr usr/pwd@LT45 control=" + ctrlFileName + " log=" + logFileName ;

   std::cout << "Executing Command: " << cmd << std::endl ;

   FILE* pipe = popen(cmd.c_str(), "r");

   if (pipe == NULL)
   {
     return -1;
   }

   char buffer[128];
   std::string result = "";

   while(!feof(pipe))
   {
     if(fgets(buffer, 128, pipe) != NULL)
     {
         result += buffer;
     }
  }

   std::cout << "Results: " << std::endl << result << std::endl ;

   pclose(pipe);
}