Bubblesort reading and sorting of csv file in C

Asked

Viewed 122 times

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

1 answer

0

Managed to finish?

  • The first part is to define an array of these structures and load the whole file into memory, a record in each structure. The simplest is to create a vector of pointers for structures and load from the CSV file. Deep down this is true:
typedef char** ** CSV;

A csv file can be described as such. But you can use the structure as you find easier. Don’t worry about proper names and such, because it will only load and sort. It’s not like you’re actually going to process the data. Your exercise is much simpler.

  • Write a function to show the structure on the screen and use it to test. Do not write more than that and do not continue before this is right, Bubble Sort works "in-place" so it has to be all in memory, an aligned structure.

  • type a program that takes an int vector and sorts via Bubble Sort

  • change the program to sort a pointer vector. The only difference is that when comparing the values on the Sort you compare the structures from the pointers and not the value of the pointers themselves.

I think I posted one or more of those things on this forum. If not finished answer and I can try to help more or post an example

Browser other questions tagged

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