Read .txt separated by commas and save in a List or pointer to char in C

1

I have a file in which I have country information in this way: Mexico, Mexico Df, Felipe Calderon, Mexican peso Other country, another capital, the president, currency ... etc

and I want to save them in a variable and then put it into a structure, I have this

FILE *f;
f=fopen("mundo.txt", "r");

char pal[100];
memset(pal, '
FILE *f;
f=fopen("mundo.txt", "r");

char pal[100];
memset(pal, '%pre%', 100);

int cont=0;
for(i=0; i<TAM; i++){

fgets(pal, 100,f); //leo la linea

char *pais, *capital, *presidente, *moneda, *idioma; //la separo con strtok
pais=strtok(pal, ","); 
capital=strtok(NULL, ",");
presidente=strtok(NULL, ",");
moneda=strtok(NULL, ",");
idioma=strtok(NULL, ",");

int aux=indices[i]; //obtengo unos indices para mi arreglo de Listas
if(NumElem(arreglo[aux]) == 0){
    //Ingreso los elementos en mi lista
    arreglo[aux]=cons(pais, capital, presidente, moneda, idioma, NULL);
}

printf(" \n");

}

fclose(f);
', 100); int cont=0; for(i=0; i<TAM; i++){ fgets(pal, 100,f); //leo la linea char *pais, *capital, *presidente, *moneda, *idioma; //la separo con strtok pais=strtok(pal, ","); capital=strtok(NULL, ","); presidente=strtok(NULL, ","); moneda=strtok(NULL, ","); idioma=strtok(NULL, ","); int aux=indices[i]; //obtengo unos indices para mi arreglo de Listas if(NumElem(arreglo[aux]) == 0){ //Ingreso los elementos en mi lista arreglo[aux]=cons(pais, capital, presidente, moneda, idioma, NULL); } printf(" \n"); } fclose(f);

When I try to access an element of my arrangement they all carry the same line with garbage, example: Zimbabwe we a re olar de Zimbabwe bwe

My structure receives char * elem;

I hope you understand my message, thank you

    
asked by Isaias Pacheco 05.06.2018 в 03:13
source

0 answers