How to read a . csv file in C?

Asked

Viewed 1,349 times

0

I created this code in C where I inserted the coordinates of some points and with SVG I drew those points in the space connected by lines. all saved in a. html file to be executed in the browser. It’s like a route schema where I want to calculate between points.

The question is: So far I have inserted the points directly in my code. Now I want to go up and read the X and Y points of another file I created. csv, draw them in SVG and connect them through lines, just as I have done so far. And when changing the file I want the program to update automatically.

In addition to the help, tips for optimization and improvement are also welcome, because I am a beginner in programming.

The following code is working and is an example of what I want to do:

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

#define MyFileName "MEUARQUIVOSVG.html"
#define MapSize 500

int main (int argc, char *argv[])
{
    FILE *html = fopen(MyFileName,"w+");
    int coordenadas[5][2]={{50,50},{100,100},{150,320},{100,250},{400,300}};
    char string[1024];
    int i;

    fprintf(html,"<html> \n <body>\n");


    fprintf(html,"<h1>SVG em C</h1>");

    fprintf(html,"<svg width=\"%d\" height=\"%d\">\n",MapSize,MapSize);
    fprintf(html,"<rect width=\"%d\" height=\"%d\" style=\"fill:rgb(255,255,255);stroke-width:5;stroke:rgb(0,0,0)\" />\n",MapSize,MapSize);

    for (i=0;i<5;i++) {
        fprintf(html,"<circle cx=\"%d\" cy=\"%d\" r=\"3\" fill=\"blue\" />\n",coordenadas[i][0],coordenadas[i][1]);
        sprintf(string,"%s%d,%d ",string,coordenadas[i][0],coordenadas[i][1]);
    }
    for (i=0;i<4;i++) {
        fprintf(html,"<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" style=\"stroke:rgb(255,0,0);stroke-width:2\" />\n", coordenadas[i+1][0],coordenadas[i+1][1], coordenadas[i][0],coordenadas[i][1]);
    }
    fprintf(html,"<polyline points=\"%s\" style=\"fill:white;stroke:red;stroke-width:1\"",string);
    fprintf(html,"</svg>\n");


    fprintf(html,"</body> \n </html>");

    fflush(html), fclose(html);
}

From now on, thank you!

  • what have you tried ? what problems you found ? your question is very generic..

  • Have you tried to open your file .csv in the notepad or some other text editor, to see what his face looks like?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.