[英]fprintf not writing to the pipe
我使用 _popen 成功打開了 pipe 即 gnuplot window。 但無法使用 fprintf 寫入 stream。 我檢查了文件指針值,它不是 null。我搜索了很多資源並使用了 fflush,但它不起作用。 我找不到解決辦法。
實際上我之前在這里gnuplot c++ interface through pipes -cannot open wgnuplot reposting with some modifications 之前問過類似的問題。
任何的意見都將會有幫助..
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打開但沒有output plot。
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;
}
我認為問題不在於您的代碼,而是 -persist 在 windows 上沒有執行任何操作。這意味着該圖的顯示時間不會超過一瞬間。 您可以生成如下所示的圖像,而不是顯示 gnuplot window。
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE* gp;
gp = _popen("gnuplot", "w");
if (gp == NULL)
return -1;
fprintf(gp, "set terminal png\nset output 'tmp.png'\n");
fprintf(gp, "set isosample 100\n");
fprintf(gp, "min=-1\n");
fprintf(gp, "max=1\n");
fprintf(gp, "pi=3.141592\n");
fprintf(gp, "set hidden3d\n");
fprintf(gp, "set pm3d\n");
fprintf(gp, "set contour\n");
fprintf(gp, "splot [min:max] [min:max] x*x+2*y*y-0.3*cos(3*pi*x)-0.4*cos(4*pi*y)+0.7\n");
fflush(gp);
pclose(gp);
return 0;
}
或者你可以引入某種等待。
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE* gp;
gp = _popen("gnuplot", "w");
if (gp == NULL)
return -1;
fprintf(gp, "set isosample 100\n");
fprintf(gp, "min=-1\n");
fprintf(gp, "max=1\n");
fprintf(gp, "pi=3.141592\n");
fprintf(gp, "set hidden3d\n");
fprintf(gp, "set pm3d\n");
fprintf(gp, "set contour\n");
fprintf(gp, "splot [min:max] [min:max] x*x+2*y*y-0.3*cos(3*pi*x)-0.4*cos(4*pi*y)+0.7\n");
fflush(gp);
for (;;) { }
return 0;
}
您對 fprintf 的語法使用是錯誤的。 它是
fprintf(FILE,"[string literals] [format place holder]",[arguments corresponding to each place holder]);
占位符的語法是
%[format type]
對於格式類型,
i or d = int
e = double shown in exponential form
f = double shown in floating point notation
g = double shown in best format
s = char * (a string literal)
c = char
舉個例子:
int i=5;
fprintf("i is equal to: %i\n",i);
會 output:
i is equal to 5
\n 是顯示新行的轉義序列。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.