Thread: Read CSV file in C

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    4

    Read CSV file in C

    Hi guys!
    I'm new into the programming in C.
    I have a issue with a program that i need to do.
    I've got those csv files:

    stock.csv
    Botines 1 30 1200
    Runners 2 18 850
    Zapatillas 3 27 800

    distribuidores.csv
    Nike 1 Capital
    Adida 2 La Plata
    Reebok 3 Cordoba
    Puma 4 Mendoza

    This is the program that i did.
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <conio.h>
    
    
    void extraeStock(char *linsto, char *descsto, int *cdpro ,int *cantsto, double *precio){
    char *cadcdPro, *cadCant, *cadPrecio, *delimit;
    delimit=strtok(linsto,",;");/*delimitadores*/
    strcpy(descsto,delimit);
    cadcdPro=strtok(NULL,";");
    *cdpro=atoi(cadcdPro);
    cadCant=strtok(NULL,";");
    *cantsto=atoi(cadCant);
    cadPrecio=strtok(NULL,";");
    *precio=atof(cadPrecio);
    }
    
    
    void extraeDistri(char *lindis, char *ape, char *codloc ,int *coddis){
    char *cadApe, *cadcodLoc, *cadcodDis, *delimit;
    delimit=strtok(lindis,";");
    strcpy(ape,lindis);
    strcpy(codloc,lindis);
    cadcodDis=strtok(NULL,";");
    *coddis=atoi(cadcodDis);
    }
    
    
    int main(){
    
    
    FILE *stock=fopen("stock.csv","r");
    FILE *distri=fopen("distribuidores.csv","r");
    char descsto[50], ape[50], codloc[50];
    char linsto[2000], lindis[2000];
    int cdpro, cant, coddis;
    double precio;
    
    
    if(stock!=NULL){
                    fgets(linsto,2000,stock);
                    printf( "Cod.Prod.\tCantidad\tDesc.Prod.\tPrecio./U\n");
                            while(!feof(stock))
                            {
                            extraeStock(linsto,descsto,&cdpro,&cant,&precio);
                            printf("%d \t\t%d \t\t%s \t$ %.2lf\n",cdpro,cant,descsto,precio);
                            fgets(linsto,2000,stock);
                                }
                    }
    if(distri!=NULL){
                    fgets(lindis,2000,distri);
                    printf( "\nCod.Distri. \tEmpresa\t\tLocalidad\n");
                            while(!feof(distri))
                            {
                            extraeDistri(lindis,ape,codloc,&coddis);
                            printf("%d \t\t%s \t\t%s\n",coddis,ape,codloc);
                            //fscanf(distri,"%s \t\t%d \t\t%s\n",ape,&coddis,codloc);
                            //printf("%d \t\t%s \t\t%s\n",coddis,ape,codloc);
                            fgets(lindis,2000,distri);
                                }
                    }
    system("pause");
    return 0;
    }
    The first function (extraeStock) it's running ok, it's bring me that i need. But the second its not working, it's repeating the same column twice.

    Read CSV file in C-thump_9800474sin-ttulo-pngRead CSV file in C-sin-título-png
    http://www.subirimagenes.com/imageda...4sin-ttulo.png
    Read CSV file in C-sin-título-png

    Any know how can i solved this?
    Last edited by padefi; 09-19-2017 at 01:25 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-07-2014, 07:01 AM
  2. Replies: 7
    Last Post: 12-07-2012, 10:44 PM
  3. Open a file, read it ... and ... read it again
    By Tiago in forum C Programming
    Replies: 1
    Last Post: 04-17-2010, 03:32 AM
  4. How can I know the actual bytes read in a file read
    By pliang in forum C++ Programming
    Replies: 1
    Last Post: 06-08-2005, 04:23 PM

Tags for this Thread