-
2 Attachment(s)
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.
Attachment 15206Attachment 15205
http://www.subirimagenes.com/imageda...4sin-ttulo.png
Attachment 15205
Any know how can i solved this?
-
> 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);
}
-
I've tried to fix them but i couldn't solve.
How can i print codloc (or another string) whitout step on ape?
-
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.
-
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!
-
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'.