gnuplot无法读取.dat文件

时间:2015-02-01 06:43:40

标签: c windows visual-studio-2012 gnuplot

我从2天开始就遇到了这个错误'gnuplot:无法读取data.dat'错误。

我也放了文件路径,但错误仍然存​​在。我搜索了互联网,但我没有得到它。

谢谢

void plotgraph(float *xvals, float *yvals, float *x1vals, int NUM_POINTS)
{

 int NUM_COMMANDS = 4;
//char * commandsForGnuplot[] = { "set title \"Concatenated Coding+OFDM[QPSK]\"", "set ylabel 'BER'", "set xlabel 'SNR'", "plot '\C:\\Users\\shreyasn\\Documents\\Visual Studio 2013\\Projects\\Project1\\Project1\\data.temp\' with lines" };
//FILE * temp = fopen_s(&temp, "%temp%\\data.temp", "w");
//char *commandsForGnuplot[] = { "set title \"Concatenated Coding+OFDM[QPSK]\"", "set ylabel 'BER'", "set xlabel 'SNR'", "set logscale y", "set nologscale x", "plot 'data.temp' with lines title 'After coding' , \ 'data.temp1' with lines title 'Before coding'" };
// double xvals[NUM_POINTS] = {1.0, 2.0, 3.0, 4.0, 5.0};
//double yvals[NUM_POINTS] = {5.0 ,3.0, 1.0, 3.0, 5.0};
int i;
for (i = 0; i < NUM_POINTS; i++)
{
    printf("time: %f decod: %f encod: %f \n", xvals[i], x1vals[i], yvals[i]); //Write the data to a temporary file
}

errno_t err;
FILE *pipe;
FILE *temp9;
if ((err = fopen_s(&temp9, "data.dat", "w+")) != 0)
    printf("File not opened\n");
if (temp9 == NULL) { 
    printf("Error\n");
}

char * commandsForGnuplot[] = { "set title \"Concatenated Coding+OFDM[QPSK]\"", "set ylabel 'BER'", "set xlabel 'SNR'","plot '\C:\\Users\\shreyasn\\Documents\\Visual Studio 2013\\Projects\\Project1\\Project1\\data.dat\' using 1:2 with lines" };
//char * commandsForGnuplot[] = { "set title \"Concatenated Coding+OFDM[QPSK]\"", "set ylabel 'BER'", "set xlabel 'SNR'", "plot 'data.dat' with lines" };
//FILE * temp1 = fopen_s(&temp1,"data.temp1", "w");
//char *path = "C:\\Program Files (x86)\\gnuplot\\bin";
pipe = _popen("\"C:\\gnuplot\\binary\\gnuplot.exe\" -persistent", "w");
//Opens an interface that one can use to send commands as if they were typing into the
//    gnuplot command line.  "The -persistent" keeps the plot open even after your
//    C program terminates.
//




for (i = 0; i < NUM_POINTS; i++)
{
    fprintf(temp9, "%f %f \n", xvals[i], yvals[i]); //Write the data to a temporary file
    //fprintf(temp1, "%lf %lf \n", xvals[i], x1vals[i]); //Write the data to a temporary file
}

fclose(temp9);
fflush(temp9);

for (i = 0; i < NUM_COMMANDS; i++)
{
    fprintf(pipe, "%s \n", commandsForGnuplot[i]); //Send commands to gnuplot one by one.
}
fflush(pipe);

}

2 个答案:

答案 0 :(得分:0)

the following information, copied from elsewhere, 
it shows how to open a pipe.

prototype:
int pipe(intfd[2]);                                             
RETURNS: 0 success                                                       
         -1 on error: and set errno 

注意:fd [0]设置为读取,fd [1]设置为写入

数组中的第一个整数(元素0)被设置并打开以供读取,而第二个整数(元素1)被设置并打开以进行写入。从视觉上讲,fd1的输出成为fd0的输入。再次,通过管道传输的所有数据都会在内核中移动。

    #include <stdio.h>
    #include <unistd.h>
    #include <sys/types.h>

    main()
    {
            int     fd[2];

            pipe(fd);
            .
            .
    }

答案 1 :(得分:0)

fopen_s上的文档说明了模式字符串:    (还有其他允许的模式字符串,但以下似乎     最有用的)

“R +”     打开阅读和写作。 (该文件必须存在。)

“W +”     打开一个空文件进行读写。     如果文件存在,则其内容将被销毁。

So I suggest using the "r+" mode string rather than the "w+" mode string

the failure to open suggests that the user does not have
permission to overwrite the file.