I successfully open the handset in the gnuplot window using _popen. But failed to write stream using fprintf. I checked the value of the file pointer and it is not zero. I searched a lot of sources and used fflush and it does not work. I could not find a solution.
Actually I asked a similar question before this gnuplot C ++ interface via the -cannot open wgnuplot channels with some changes.
Any suggestions would be helpful.
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);
I used this code without using pipes and it uses createprocess. The same situation is here, Gnuplot.exe opens, but without output.
int _tmain (int argc, LPTSTR argv [])
{
DWORD i;
HANDLE hReadPipe, hWritePipe;
SECURITY_ATTRIBUTES PipeSA = {sizeof (SECURITY_ATTRIBUTES), NULL, TRUE};
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;
GetStartupInfo (&StartInfoCh2);
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);
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);
CloseHandle (ProcInfo2.hProcess);
return 0;
}