Piping EOF problems with stdio and C++/Python
- by yeus
I got some problems with EOF and stdio. I have no idea what I am doing wrong. When I see an EOF in my program I clear the stdin and next round I try to read in a new line. The problem is: for some reason the getline function immediatly (from the second run always, the first works just as intended) returns an EOF instead of waiting for a new input from py python process... Any idea?
alright Here is the code:
#include <string>
#include <iostream>
#include <iomanip>
#include <limits>
using namespace std;
int main(int argc, char **argv) {
for (;;) {
string buf;
if (getline(cin,buf)) {
if (buf=="q") break;
/*****///do some stuff with input //my actual filter program
cout<<buf;
/*****/
} else {
if ((cin.rdstate() & istream::eofbit)!=0)cout<<"eofbit"<<endl;
if ((cin.rdstate() & istream::failbit)!=0)cout<<"failbit"<<endl;
if ((cin.rdstate() & istream::badbit)!=0)cout<<"badbit"<<endl;
if ((cin.rdstate() & istream::goodbit)!=0)cout<<"goodbit"<<endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max());
//break;//I am not using break, because I
//want more input when the parent
//process puts data into stdin;
}
}
return 0;
}
and in python:
from subprocess import Popen, PIPE
import os
from time import sleep
proc=Popen(os.getcwd()+"/Pipingtest",stdout=PIPE,stdin=PIPE,stderr=PIPE);
while(1):
sleep(0.5)
print proc.communicate("1 1 1")
print "running"