popen - immediate EOF after fgets

2019-09-07 15:14发布

问题:

I am having troubles with popen (I am using this on a CentOS machine).

The code is like so (_oss is an ostringstream):

  ...

  FILE*  lp_pipe = popen("/opt/scripts/myscript.php", "r");

  if(!lp_pipe) {
    return false;
  } 

  char l_buffer[256];
  while(!feof(lp_pipe))
  {  
    if(fgets(l_buffer, 256, lp_pipe) != NULL) 
      _oss << l_buffer;
  }

  pclose(lp_pipe);

  ....

Now this was working fine until someone changed the php script. When I run the script from the commandline I still see the expected result, however the code doesn't work anymore. The first time fgets is called it now returns NULL and afterwards feof(lp_pipe) returns 1

标签: c++ c posix