Thread View: borland.public.cppbuilder.nativeapi
1 messages
1 total messages
Started by Rich C
Sun, 18 May 2008 22:23
Problem with redirection of standard input and output
Author: Rich C
Date: Sun, 18 May 2008 22:23
Date: Sun, 18 May 2008 22:23
67 lines
3113 bytes
3113 bytes
I have a console program that prompts for input and outputs to the
console. I
am writing an interactive non-console program that will generate the
inputs
and using pipes (to replace standard input and output) will send the
input to
the console program and write the output of the console program to a
file.
The problem I have is that I need the call to the Sleep function to
make this
work. Otherwise, I do not get all the output. The console program is a
C++
program that uses cout to write the output and from what I read is
that cout
uses buffering and possibly that is the cause of the problem.
Is there any other way other than using Sleep to get all the output.
An example of the section of code that does this:
char Data[1024];
static char *data_in[] =
{
"5\n",
"1\n",
"a\n",
"d\n",
"a\n",
"3\n",
" \n",
"m\n"
};
file_out = fopen("test_out","wb");
for(i=0; i<sizeof(data_in)/sizeof(data_in[0]); i++)
{
/* Send Input.
WritePipeHandle2 is the pipe used for STDIN in the console
program */
if(!
WriteFile(WritePipeHandle2,data_in[i],strlen(data_in[i]),&BytesRead,NULL))
RaiseLastWin32Error();
Sleep(25); // This seems to be necessary but does not always
work
/* Read output and write to file.
ReadPipeHandle1 is the pipe used for STDOUT in the console
program */
while(1)
{
if(!
PeekNamedPipe(ReadPipeHandle1,Data,sizeof(Data),&BytesToRead,
&TotalBytes,&BytesLeft))
RaiseLastWin32Error();
if(BytesToRead == 0)
break;
if(!
ReadFile(ReadPipeHandle1,Data,BytesToRead,&BytesRead,NULL))
RaiseLastWin32Error();
fwrite(Data,BytesRead,1,file_out);
}
}
fclose(file_out);
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads