EDITED
First time here!
I am trying to split a line in words and save them in an array of 13 positions. I have a CSV file (bcancer.csv) separated with ; with 13 columns.
The file contains 700 lines like this ones with obviously different info (no blank lines at all):
1000025;0;89;5;1;1;1;2;1;3;1;1;2
1002945;0;71;5;4;4;5;7;10;3;2;1;2
1015425;0;55;3;1;1;1;2;2;3;1;1;2
1016277;0;76;6;8;8;1;3;4;3;7;1;2
1017023;0;91;4;1;1;3;2;1;3;1;1;2
And I don't know if I should use a 2D array or unidimensional, because I have to print all the info later in another function.
As for now, I have this code:
#include<stdio.h>
#include<string.h>
int muestraMenu ();
void opcion0 ();
int main ()
{
int opcion = 0;
do
{
opcion = muestraMenu ();
printf ("\tOpción elegida: %d\n", opcion);
switch (opcion)
{
case 0:
printf (" 0. Configuración e inicialización");
opcion0();
break;
}
}
while (opcion != 0);
return 0;
}
int
muestraMenu ()
{
int opcion;
printf ("\n 0. Configuración e inicialización");
printf ("\n\n Introduzca opción: ");
scanf ("%d", &opcion);
return opcion;
}
void opcion0 ()
{
char line[100] = "";
char *datos_pac[13];
int i = 0;
FILE *f = fopen ("bcancer.csv", "r");
while(!feof(f))
{
fgets(line,100,f);
datos_pac[i] = strtok(line, ";"); //Guardo primer elemento
while(datos_pac[i] != NULL) //Guardo el resto de elementos
{
datos_pac[++i] = strtok(NULL, ";");
printf("%s ",datos_pac[i-1]);
}
}
}
But it's not working,the first lines are printed wrong and second one it's not printed at all. Also after 50 lines it stopped.
For the output I expect to save all the info in the lines in an array.
datos_pac[0][0]=1000025
datos_pac[0][1]=0
datos_pac[0][2]=89
and so on...
Please let me know where I'm going wrong, thanks!