Thread: Read CSV file in C

  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.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > strcpy(ape,lindis);
    > strcpy(codloc,lindis);
    So why do you make 2 copies of the string here?

    Are you missing another strtok()?

    Your indentation needs work.
    Indent style - Wikipedia

    You can save yourself some fgets/feof madness by simply doing
    Code:
    while ( fgets(linsto,sizeof(linsto),stock) ) {
        extraeStock(linsto,descsto,&cdpro,&cant,&precio);
        printf("%d \t\t%d \t\t%s \t$ %.2lf\n",cdpro,cant,descsto,precio);
    }
    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.

  3. #3
    Registered User
    Join Date
    Sep 2017
    Posts
    4
    I've tried to fix them but i couldn't solve.
    How can i print codloc (or another string) whitout step on ape?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So you're telling me this doesn't work?
    Code:
    delimit=strtok(lindis,";");
    strcpy(ape,delimit);
    delimit=strtok(NULL,";");
    strcpy(codloc,delimit);
    I'm assuming that the complete lack of ; separators in your original post of distribuidores.csv is not an actual reflection of what's in the file.
    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.

  5. #5
    Registered User
    Join Date
    Sep 2017
    Posts
    4
    It works!
    I always thought that "NULL" only can i used it with int/double, i didn't know that i could use it with string/char.
    Thanks you so much!

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    NULL is nothing to do with data types in the string.
    It simply informs strtok() to 'carry on from where it left off last time'.
    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. 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