Thread: Programm C fix

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    24

    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);
        }
    
        // write tex head
        const char *tex_head =
            "\\documentclass{report} \\usepackage{tikz} \\begin{document} "
            "\\begin{tikzpicture}[x=0.1cm,y=0.1cm] \\draw\n";
        fputs(tex_head, tex_file);
    
        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);
    }
    Last edited by jasmin89; 02-13-2021 at 04:47 AM.

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    798
    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.

    Please see man strtok for more information.

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    798
    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.
    Last edited by rstanley; 02-13-2021 at 05:23 AM.

  4. #4
    Registered User
    Join Date
    Nov 2020
    Posts
    24
    Quote Originally Posted by rstanley View Post
    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.
    Attached Files Attached Files
    Last edited by jasmin89; 02-13-2021 at 05:27 AM.

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    798
    Quote Originally Posted by jasmin89 View Post
    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. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,541
    > 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++
            }
        }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a programm plz help me!!!!
    By chaudhryali55 in forum C++ Programming
    Replies: 1
    Last Post: 11-21-2009, 07:29 AM
  2. How can i run a programm ?
    By Shirali in forum C++ Programming
    Replies: 3
    Last Post: 03-29-2009, 02:05 AM
  3. Plz help me C programm
    By ferroz1 in forum C Programming
    Replies: 7
    Last Post: 05-10-2008, 06:55 AM
  4. first C++ programm..
    By datainjector in forum C++ Programming
    Replies: 3
    Last Post: 12-08-2002, 08:30 PM
  5. help me with this programm
    By a.christodoulou in forum C++ Programming
    Replies: 1
    Last Post: 11-30-2002, 04:50 AM

Tags for this Thread