2016-05-23 6 views
2

Я написал программу, выход которой написан внутри файла (с именем large.dat), и в той же программе я пытаюсь вызвать gnuplot и начертить данные, которые являются хранится в файле large.dat. Но после того, как runnung код следующей ошибки приходитВызов gnuplot для построения файла в программе C

line 0: warning: Skipping data file with no valid points 

gnuplot> plot'bigger.dat' 
        ^
    line 0: x range is invalid 

Как мне удалить эту ошибку.

Вот мой полный код

#include <stdio.h> 
#include <stdlib.h> 

int main(void){ 

FILE *f;//pointer to a file 
int i; 
f = fopen("bigger.dat","w");// creating a file in the write mode 

/*check whether it is opened or not*/ 

if (f == NULL) 
{ 
printf("unable to open file"); 
return 0; 
} 

/* if the file is opened now then we write the results of following  program to tht file*/ 

for (i=1;i<10;i++) 

{ 

fprintf(f,"%d %d\n",i,i*i); 


} 


/* following code opens gnuplot and read data points from big.dat file*/ 
FILE *fp = fopen ("myfile","wt"); 
fprintf(f,"set xrange[0:10]\n"); 
fprintf(f, "set yrange [0:100]\n "); 
fprintf(f, "set zeroaxis\n"); 
fprintf(fp,"plot'%s'\n","bigger.dat"); 
fclose(fp); 
system("gnuplot -p <myfile"); 


return 0; 
}//main 
+0

Похоже, вы на самом деле пытаетесь получить вывод, который вы получаете, когда используете gnuplot так: 'set xrange [0:10]' then' set yrange [0:50] ', add' set zeroaxis' , а затем, наконец, сама функция: «plot x ** 2' –

+0

@ У меня был измененный код, но по-прежнему та же ошибка – vivek

ответ

-1

Из того, что я понимаю, вы хотите, чтобы прочитать содержание от bigger.dat до myfile, а затем отправить myfile в gnuplot?

В этом случае вы должны к этому же, как вы написали их вниз, то есть вы должны прочитать от bigger.dat до myfile, а затем отправить myfile в gnuplot:

int main(void) 
{ 

    FILE *f;//pointer to a file 
    int i; 
    f = fopen("bigger.dat","w");// creating a file in the write mode 

    /*check whether it is opened or not*/ 

    if (f == NULL) 
    { 
    printf("unable to open file"); 
    return 0; 
    } 

    /* if the file is opened now then we write the results of following  program to tht file*/ 

    for (i=1;i<10;i++) 

    { 

    fprintf(f,"%d %d\n",i,i*i); 


    } 

    fclose(f); 



    FILE *fp = fopen("myfile", "w"); // I'm guesssiog you want to read it? 
    f = fopen("bigger.dat", "r"); // you have to open it again for reading 
    int a,b; 

    for (i=1; i<10; i++) 
    { 
     fscanf(f, "%d %d\n", &a,&b); 
     fprintf(fp, "plot %d %d\n", a,b); 
    } 

    fclose(f); 
    fclose(fp); 

    system("gnuplot -p <myfile"); 

    return 0; 
} 

Конечно, это все-таки выиграл» т будет работать должным образом, мое предложение написать Баш скрипт для чтения myfileline by line, или (если вы не знаете Баш сценариев) проверить this answer, то есть построить команду, которую вы хотите отправить system():

#include <stdio.h>                
#include <stdlib.h>                

int main()                  
{                    
    int a;                  
    char buf[BUFSIZ];               

    printf("Please enter a number:\n");           
    scanf("%d",&a);                
    printf("Your number is: %d\n",a);           
    snprintf(buf, sizeof(buf), "echo %d",a);         
    system(buf);                
} 

В вашем случае это будет (только последняя часть):

... 
    f = fopen("bigger.dat", "r"); // open it again for reading 
    int a,b; 
    char command[30]; 

    for (i=1; i<10; i++) 
    { 
     fscanf(f, "%d %d\n", &a,&b); 
     snprintf(buf, sizeof(buf), "gnuplot plot %d %d", a,b %d",a);         
     system(command); 
     command = ""; // reset the command variable 
    } 

    fclose(f); 

    return 0; 
} 
+0

Я пробовал ваш код, но он бросает следующее err [write3.c: In function 'main': write3 .c: 38: 14: ошибка: исправление «i» без привязки для (int i = 1; i <10; i ++) ^ write3.c: 8: 9: примечание: предыдущее объявление 'i' был здесь int i; ^^ write3.c: 38: 5: ошибка: начальные объявления цикла «for» разрешены только в режиме C99 или C11 для (int i = 1; i <10; i ++) ^ write3.c: 38: 5: примечание: используйте параметр -std = c99, -std = gnu99, -std = c11 или -std = gnu11, чтобы скомпилировать код ] – vivek

+0

На самом деле моя программа пытается сделать следующие две вещи в одной программе: 1 .dat, который должен содержать результат x ** 2 И 2: gnuplot должен читать файл большего.dat для его построения. – vivek

+0

@vivek О да, извините, я отредактировал ответ, теперь должен работать. –

0

Вот код

#include <stdio.h> 
#include <stdlib.h> 

int main(void) 
{ 

FILE *f;//pointer to a file 
int i; 
f = fopen("BoosterData.dat","w");// creating a file in the write mode 

/*check whether it is opened or not*/ 

if (f == NULL) 
{ 
printf("unable to open file"); 
return 0; 
} 

/* if the file is opened now then we write the results of following  program to tht file*/ 

for (i=1;i<10000;i++) 

{ 

fprintf(f,"%d %d\n",i,i*i); 


} 

fclose(f); 




    FILE *pipe_gp = popen("gnuplot -p", "w"); 

// fputs("set terminal pn\n", pipe_gp); 
    fputs("set xlabel date\n",pipe_gp); 
    fputs("plot 'BoosterData.dat' u 1:2 title 'data1'\n", pipe_gp); 
    pclose(pipe_gp); 
    return 0; 
} 
2

Как правило, gnuplot требует участка файл, описывающий данные (столбцы) для построения и настройки различных параметров сюжета (название, осевые метки, сетка, линии и т. д.) См. How to pass command line argument to gnuplot?. Если вы хотите сделать вывод из программы C, вам придется либо иметь существующий файл сюжета, либо создать его «на лету» из вашего кода.

Кроме того, в то время как вы можете быть в состоянии pipe свой путь к решению, вы лучше служили разветвление сюжет для отдельного процесса, построение командной строки с требуемыми параметрами и вызова gnuplot из execvp или тому подобное. (который обеспечивает поиск пути в пул и т. д.). С помощью fork вы можете разгрузить график до ребенка, вызывая execvp оттуда, оставляя вашу программу свободной, не дожидаясь на gnuplot.

В следующем примере используется этот подход. Все вспомогательные функции - это просто функции проверки ошибок или просто служебные функции для разделения путей и не связаны с примерами fork, , gnuplot. (Они просто держать main() лаконичными, так что вы можете сосредоточиться на логике, не пробираясь через все валидации. (Всегда есть дополнительная валидация можно добавить).

Код записают значения, вытекающие из простой функции f(x) = x^3 - x2 к имени файла заданный как первый аргумент, предоставленный в командной строке (или до gnuplot.dat, если аргумент не указан).Программа создаст соответствующий участок файл на лета, написанной gnuplot.plt (по умолчанию) или имени файла, представленный в формате filename.plt (где plt заменяет исходное расширение)

сюжета, созданная программой является следующее:

enter image description here

Вот небольшой пример кода:

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <sys/types.h> 
#include <sys/wait.h> 
#include <unistd.h> 

enum { MAXL = 128, MAXC = 256}; 

FILE *xfopen (const char *fn, const char *mode); 
int badmode (const char *s); 
int xfclose (FILE *fp); 
int xfexists (char *fn); 
char *fnwoext (char *nm, char *fn); 

int main (int argc, char **argv) { 

    int pid, status; 
    float f[MAXL] = {0.0}; 
    char *fn = argc > 1 ? argv[1] : "gnuplot.dat"; 
    char fnbase[MAXC] = "", fnplt[MAXC] = ""; 
    size_t i; 
    FILE *fp = NULL; 

    for (i = 0; i < MAXL; i++)  /* fill array of values */ 
     f[i] = i * i * i - i * i; /* x^3 - x^2. no overflow */ 

    fp = xfopen (fn, "w");  /* open output file */ 

    for (i = 0; i < MAXL; i++) /* write values to file */ 
     fprintf (fp, "%3zu %10.2f\n", i, f[i]); 

    xfclose (fp); /* close output file */ 

    /* create 'plot' file 'fn.plt' */ 
    strcpy (fnplt, fnwoext (fnbase, fn)); 
    strcat (fnplt, ".plt"); 
    if (!xfexists (fnplt)) { 
     xfopen (fnplt, "w"); 
     fprintf (fp, "set xlabel 'x'\n" 
        "set ylabel 'f(x) = x^3 - x^2'\n" 
        "set title 'Function Plot of f(x) = x^3 - x^2'\n" 
        "set grid\n" 
        "set style data lines\n" 
        "plot \"%s\" using 1:2 lw 3 linecolor rgb \"blue\"\n" 
        "quit\n", fn); 
     xfclose (fp); 
    } 

    /* fill arguments array for execvp */ 
    char *args[] = { "gnuplot", "-p", fnplt, NULL }; 

    if ((pid = (fork())) < 0) { /* fork plot process */ 
     fprintf (stderr, "fork() error: fork failed.\n"); 
     return 1; 
    } 
    else if (pid == 0) { /* plot from child process */ 
     if (execvp (*args, args) == -1) { 
      fprintf (stderr, "execvp() error: returned error.\n"); 
      _exit (EXIT_FAILURE); 
     } 
    } 

    waitpid (pid, &status, 0); /* wait for plot completion (not req'd) */ 

    return 0; 
} 

/** fopen with error checking - short version */ 
FILE *xfopen (const char *fn, const char *mode) 
{ 
    if (!fn || !mode || badmode (mode)) { 
     fprintf (stderr, "xfopen() error: invalid parameter.\n"); 
     exit (EXIT_FAILURE); 
    } 
    FILE *fp = fopen (fn, mode); 

    if (!fp) { 
     fprintf (stderr, "xfopen() error: file open failed '%s'.\n", fn); 
     exit (EXIT_FAILURE); 
    } 

    return fp; 
} 

/** validate file mode 's' is "rwa+b" */ 
int badmode (const char *s) 
{ 
    const char *modes = "rwa+b"; 

    for (; *s; s++) { 
     const char *m = modes; 
     int valid = 0; 
     while (*m) if (*s == *m++) { valid = 1; break; } 
     if (!valid) return *s; 
    } 
    return 0; 
} 

/** file close with error check */ 
int xfclose (FILE *fp) 
{ 
    if (fclose (fp)) { 
     fprintf (stderr, "xfclose() error: nonzero return on fclose.\n"); 
     return 1; 
    } 
    return 0; 
} 

/** check if file 'fn' already exists */ 
int xfexists (char *fn) 
{ 
    /* if access return is not -1 file exists */ 
    if (access (fn, F_OK) != -1) 
     return 1; 

    return 0; 
} 

/** isolate filename, without path or extension */ 
char *fnwoext (char *nm, char *fn) 
{ 
    char *p = NULL, *ep = NULL; 
    char fnm[MAXC] = ""; 

    if (!fn) return NULL; 
    strcpy (fnm, fn); 
    if ((p = strrchr (fnm, '/'))) 
     p++; 
    else 
     p = fnm; 

    if ((ep = strrchr (p, '.'))) { 
     *ep = 0; 
     strcpy (nm, p); 
     *ep = '.'; 
    } else 
     strcpy (nm, p); 

    return nm; 
} 

Посмотрите на код, подход с fork и execvp и дайте мне знать, если у вас есть дополнительные вопросы.

Смежные вопросы