1. ## Program C fix

I have measured data divided into 100 steps. I want to read this data in C and save the temperature as a latex file. It should be output with a nice temperature curve. On the Y axis the temperature should appear and on the X axis the 100 steps. But this does not work for me. Can someone fix me the code? The data file looks like this:
x y temperature
0.292886,-0.015196,21.379749
.
.
.
X and y are not important. Only temperature is important

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
FILE *input_file, *tex_file;

input_file = fopen("result.csv", "r");
if (!input_file) {
printf("fail to open result.csv\n");
exit(1);
}

tex_file = fopen("path.tex", "w");
if (!tex_file) {
printf("fail to open path.tex\n");
exit(1);
}

"\\documentclass{report} \\usepackage{tikz} \\begin{document} "
"\\begin{tikzpicture}[x=0.1cm,y=0.1cm] \\draw\n";

char line[1024];
double x, y, z;
while (fgets(line, sizeof(line), input_file)) {
char *save = line;

char *_z = strtok_r(NULL, ",", &save);
z = atof(_z);
fprintf(tex_file, "(%f,%f,%f)--",z);
}
fprintf(tex_file, "(%f,%f,%f);\n", z);

// write tex tail
const char *tex_tail = "\\end{tikzpicture} \\end{document}";
fputs(tex_tail, tex_file);

fclose(input_file);
fclose(tex_file);
}

2. You would first need to provide a sample data file so the code could be tested.
Without compiling the code, the one thing I see is your call to strtok_r() on line 31. You need to pass the source string as the first parameter ti the first call to strtok_r(), then NULL on subsequent calls.

3. Other errors when compiled in gcc v. 10.2.1:
Code:
foo.c:31:20: warning: initialization of ‘char *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
foo.c:33:33: warning: format ‘%f’ expects a matching ‘double’ argument [-Wformat=]
33 |         fprintf(tex_file, "(%f,%f,%f)--",z);
|                                ~^
|                                 |
|                                 double
foo.c:33:36: warning: format ‘%f’ expects a matching ‘double’ argument [-Wformat=]
33 |         fprintf(tex_file, "(%f,%f,%f)--",z);
|                                   ~^
|                                    |
|                                    double
foo.c:35:29: warning: format ‘%f’ expects a matching ‘double’ argument [-Wformat=]
35 |     fprintf(tex_file, "(%f,%f,%f);\n", z);
|                            ~^
|                             |
|                             double
foo.c:35:32: warning: format ‘%f’ expects a matching ‘double’ argument [-Wformat=]
35 |     fprintf(tex_file, "(%f,%f,%f);\n", z);
|                               ~^
|                                |
|                                double
foo.c:27:15: warning: unused variable ‘y’ [-Wunused-variable]
27 |     double x, y, z;
|               ^
foo.c:27:12: warning: unused variable ‘x’ [-Wunused-variable]
27 |     double x, y, z;
|            ^
strtok_r() may not be available on all systems.

4. Originally Posted by rstanley
You would first need to provide a sample data file so the code could be tested.

Here is the .csv file. I get no compiling error.

5. Originally Posted by jasmin89
Here is the .csv file. I get no compiling error.
Make sure you turn on Warnings, and insure your warning level is turned up to the highest level to see all warnings and errors in your code!

6. > On the Y axis the temperature should appear and on the X axis the 100 steps.
So you should only be outputting pairs of numbers to begin with.

Using strtok is messy, because you also need to have a loop to step over the first pair of values you're not interested in.

sscanf is much simpler.
Code:
    char line[1024];
double x = 0, temperature;
while (fgets(line, sizeof(line), input_file)) {
if ( sscanf(line,"%*f,%*f,%f", &temperature) == 1 ) {
fprintf(tex_file, "(%f,%f)--",x,temperature);
x++
}
}