2012-04-11 63 views
3

我成功地使用_popen打开管道,即gnuplot窗口。但无法使用fprintf写入流。我检查了文件指针值,它不是null。我搜索了很多资源并使用了fflush,但它不起作用。我找不到解决方案。fprintf不写入管道

其实我在这里问了一个类似的问题,在这里gnuplot c++ interface through pipes -cannot open 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; 
} 
+0

这可能有助于调试检查'fprintf'的返回值,如果该值为负,请使用'perror()' – keety 2012-04-11 04:51:27

+1

为什么“wt”而不是“w”? – 2012-04-11 04:52:32

+0

@VaughnCato我只是尝试不同的选项,因为在这个msdn文章[链接](http://msdn.microsoft.com/en-us/library/aa298534(v = vs.60).aspx) – ShivShambo 2012-04-11 05:10:37

回答

0

我觉得的问题不在于你的代码,但-persist没有做任何的窗户。这意味着图表从不会在一瞬间显示出来。而不是显示gnuplot窗口,您可以生成一个图像,如下所示。

#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; 
} 
+0

谢谢Bownie ..我试过这个code.But没有改变的输出。在gp = _popen(path,“w”)之后,gnuplot窗口打开,之后没有输出。它本身并不关闭。我必须每次关闭它。 – ShivShambo 2012-04-11 06:38:19

+0

..GNUPlot窗口说它用于Windows 32位。我的是64位..这是一个原因吗? – ShivShambo 2012-04-11 06:39:40

+0

谢谢大家..这是一个愚蠢的错误..我用gnuplot而不是wgnuplot。它正在工作。 正确的路径应该是这样的 char * path =“\”C:\\ Program Files \\ gnuplot \\ bin \\ gnuplot \“”; – ShivShambo 2012-04-11 07:28:44

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); 

将输出:

i is equal to 5 

\ n是一个转义序列来显示一个新行。

+1

由于'command'字符串中没有百分比符号,所以使用的应该可以正常工作。有点不正统;海湾合作委员会将警告它。但不是非法的。 format参数只能是一个字符串;它不一定是字符串文字。 – 2012-04-11 06:02:32