I will start with a brief explanation of my program, then move on to my question. I created a bi-directional handset that does the following:
parent process: CHILD PROCESS: TEST DATA
and these are my c ++ and python code codes:
test.cc:
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/wait.h>
#include <cstdlib>
int main()
{
int writepipe[2] = {-1,-1};
int readpipe[2] = {-1,-1};
pid_t childpid;
if(pipe(readpipe) < 0 || pipe(writepipe) < 0)
{
printf("error creating pipe");
exit(-1);
}
#define PARENT_READ readpipe[0]
#define CHILD_WRITE readpipe[1]
#define CHILD_READ writepipe[0]
#define PARENT_WRITE writepipe[1]
if((childpid=fork())<0)
{
printf("cannot fork child");
exit(-1);
}
else if (childpid==0)
{
close(PARENT_WRITE);
close(PARENT_READ);
dup2(CHILD_READ,0);
dup2(CHILD_WRITE , 1);
system("python test.py");
close(CHILD_READ);
close(CHILD_WRITE);
}
else
{
close(CHILD_READ);
close(CHILD_WRITE);
write(PARENT_WRITE,"TEST DATA\n",23);
int count;
char buffer [40];
count=read(PARENT_READ,buffer,40);
printf("parent process: %s",buffer);
}
return 0;
}
test.py:
import sys
data=sys.stdin.readline()
sys.stdout.write("CHILD PROCESS: "+data)
My question is:
I have a text file (let's call it test.txt) that contains several lines of data, and I want to be able to use the previous code, but instead of sending one string value (TEST DATA) I want to send the contents of the whole text file .. Any hints?
source
share