Article View: borland.public.cppbuilder.nativeapi
Article #270Problem with redirection of standard input and output
From: 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);
Technical Details
Message-ID:
<c1168b89-54d5-4934-9135-53f50c21f07b@u12g2000prd.googlegroups.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!postnews.google.com!u12g2000prd.googlegroups.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,b0e0d4df136168a3,start X-Google-Attributes: gid10cc67,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: arc1-ca.pugleaf.net!g2news1.google.com!postnews.google.com!u12g2000prd.googlegroups.com!not-for-mail From: Rich C <richard.casten@aero.org> Newsgroups: borland.public.cppbuilder.nativeapi Subject: Problem with redirection of standard input and output Date: Sun, 18 May 2008 15:23:27 -0700 (PDT) Organization: http://groups.google.com Lines: 67 Message-ID: <c1168b89-54d5-4934-9135-53f50c21f07b@u12g2000prd.googlegroups.com> NNTP-Posting-Host: 130.221.224.7 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: posting.google.com 1211149407 17902 127.0.0.1 (18 May 2008 22:23:27 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Sun, 18 May 2008 22:23:27 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: u12g2000prd.googlegroups.com; posting-host=130.221.224.7; posting-account=jTo32QoAAAAojHKEMaRNWiXdrx69l6O3 User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648),gzip(gfe),gzip(gfe)