I have a java app here that starts a C++ app via the java.lang.Process API and then tries to send commands to it via the stdin pipe:
process.getOutputStream().write("foo\n");
process.getOutputStream().flush();
On the C++ side there's a loop running that checks for input in stdin and if there is some it reads it. Unfortunately the checking always returns 0, hence it never tries to read. If I remove the check then it'll suddenly start to see the commands and process them. This is on linux.
The C++ apps code to check and read from stdin is this:
fd_set fds;
FD_ZERO ( &fds );
FD_SET ( 0, &fds );
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 0;
if( select ( 1, &fds, 0, 0, &tv ) > 0 ) {
char buf[16384];
buf[16383] = '\0';
if ( fgets ( buf, sizeof ( buf ) - 1, stdin ) == 0 )
return;
}
As I said, removing the if clause makes it work, but of course thats not so nice as the loop around it also does some other things. Anybody got an idea what I'm doing wrong here?
Update: Meanwhile I was able to reproduce the problem with two very small sample apps. The problem seems to be related to the Qt framework here, as soon as I create the QCoreApplication instance needed for the framework then select() for stdin doesn't seem to work anymore.
fgets ( buf, sizeof ( buf ), stdin )notsizeof ( buf ) - 1- user102008