0
I am doing a work for the course and one of the questions is to make a code in C to: read a csv, sort it (using the Bubblesort method) and print on screen.
I have this code that serves as a basis, but I have no idea how to solve it and put mine csv.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#define BSIZE 1000
// esse é o registro, ter q ajeitar as colunas do dataset.
typedef struct registro {
char segment[20];
char country[20];
char product[20];
char discoundBand[20];
float unitsSold;
float manufacturingPrice;
float salePrice;
float grossSale;
float discounts;
float sales;
float cogs;
float profit;
int monthNumber;
char monthName[20];
int year;
}Registro;
typedef struct no {
struct registro data;
struct no *prox;
}No;
No c;
No *p;
void imprimaCabeca (No *le) {
No *p;
for (p = le->prox; p != NULL; p = p->prox)
printf("%10.2f\n", p->data.manufacturingPrice);
}
int main (void) {
setlocale(LC_ALL, "Portuguese");
char filename[] = "cursos.csv";
FILE *fp = fopen(filename, "r");
No *le;
le = malloc (sizeof (No));
le->prox = NULL;
printf ("sizeof (node) = %d\n", sizeof (le));
if (fp == NULL){
printf("Não é possível abrir o arquivo %s",
filename);
exit(1);
}
char parsedLine[BSIZE];
char *field;
while (fgets(parsedLine, BSIZE, filename) != NULL){
No *nova;
nova = malloc (sizeof (No));
char *seg = strtok(parsedLine, ",");
strcpy(nova->data.segment, seg);
char *cou = strtok(parsedLine, ",");
strcpy(nova->data.country, cou);
char *prod = strtok(parsedLine, ",");
strcpy(nova->data.product, prod);
char *band = strtok(parsedLine, ",");
strcpy(nova->data.discoundBand, band);
field = strtok(parsedLine, ",");
float sold=atof(field);
nova->data.unitsSold = sold;
field = strtok(parsedLine, ",");
float manu=atof(field);
nova->data.manufacturingPrice = manu;
field = strtok(parsedLine, ",");
float price=atof(field);
nova->data.salePrice = price;
field = strtok(parsedLine, ",");
float gross=atof(field);
nova->data.grossSale = gross;
field = strtok(parsedLine, ",");
float disc=atof(field);
nova->data.discounts = disc;
field = strtok(parsedLine, ",");
float sale=atof(field);
nova->data.sales = sale;
field = strtok(parsedLine, ",");
float cog=atof(field);
nova->data.cogs = cog;
field = strtok(parsedLine, ",");
float prof=atof(field);
nova->data.profit = prof;
field = strtok(parsedLine, ",");
int nrm=atoi(field);
nova->data.monthNumber = nrm;
char *mes = strtok(parsedLine, ",");
strcpy(nova->data.monthName, mes);
field = strtok(parsedLine, ",");
int ano=atoi(field);
nova->data.year = ano;
nova->prox = le;
le = nova;
printf("%s:\n",
nova->data.country);
}
fclose(fp);
return 1;
}
My csv is this http://dados.gov.br/dataset/frota-de-veiculos-iffar