我使用_popen 成功打开了管道,即gnuplot 窗口。但无法使用 fprintf 写入流。我检查了文件指针值,它不为空。我搜索了许多来源并使用了 fflush ,但它不起作用。我找不到解决方案。
实际上,我在这里 通过管道的 gnuplot c++ 接口之前问了一个类似的问题 - 无法打开 wgnuplot重新发布并进行一些修改。
任何的意见都将会有帮助..
FILE* gp;
string command = "set style data lines\n" ;
char *path = "\"C:\\Program Files\\gnuplot\\bin\\wgnuplot\" -persist";
gp = _popen(path , "wt");
if (gp == NULL)
return -1;
fprintf(gp,command );
fflush(gp);
_pclose(gp);
我在不使用管道的情况下使用了这段代码,它使用了 createprocess。这里也是同样的情况,gnuplot.exe 打开但没有输出图。
int _tmain (int argc, LPTSTR argv [])
{
DWORD i;
HANDLE hReadPipe, hWritePipe;
SECURITY_ATTRIBUTES PipeSA = {sizeof (SECURITY_ATTRIBUTES), NULL, TRUE};
/* Init for inheritable handles. */
TCHAR outBuf[ ] = TEXT("a=2; plot sin(a*x)/x; pause mouse; plot exp(-a*x); pause mouse") ;
TCHAR inBuf[80];
DWORD dwWritten, dwRead ;
BOOL bSuccess = FALSE;
PROCESS_INFORMATION ProcInfo2;
STARTUPINFO StartInfoCh2;
/* Startup info for the Gnuplot process. */
GetStartupInfo (&StartInfoCh2);
/* Create an anonymous pipe with default size.
The handles are inheritable. */
bSuccess = CreatePipe (&hReadPipe, &hWritePipe, &PipeSA, 0);
if (bSuccess == TRUE) printf("pipe created\n");
WriteFile(hWritePipe, outBuf, sizeof(outBuf), &dwWritten, NULL) ;
printf("Wrote %d bytes to Gnuplot\n", dwWritten) ;
CloseHandle (hWritePipe);
/* Repeat (symmetrically) for the child process. */
StartInfoCh2.hStdInput = hReadPipe;
StartInfoCh2.hStdError = GetStdHandle (STD_ERROR_HANDLE);
StartInfoCh2.hStdOutput = GetStdHandle (STD_OUTPUT_HANDLE);
StartInfoCh2.dwFlags = STARTF_USESTDHANDLES;
bSuccess = FALSE ;
bSuccess = CreateProcess ("C:\\Program Files\\gnuplot\\bin\\wgnuplot.exe", NULL, NULL, NULL,
TRUE,0, NULL, NULL, &StartInfoCh2, &ProcInfo2);
if (bSuccess == TRUE)
printf("Created Gnuplot Process\n" ) ;
WaitForSingleObject (ProcInfo2.hProcess, INFINITE);
CloseHandle (ProcInfo2.hThread);
CloseHandle (hReadPipe);
/* Wait for Gnuplot process to complete.*/
CloseHandle (ProcInfo2.hProcess);
return 0;
}