-1

I'm trying to separate a string read from a file by the token '#'. StrTok separates the first token correctly but when i print what is supposed to be the second token it returns nothing but (null).

I tried to change the format of the file that is being read and using an aux variable to hold the content of strtok(NULL,"#"), but the results dont change.

void ler_fich_cidades(Lista_Cidades cidade,Lista_Pontos pt){

    FILE *p;
    int file_check=0;
    char linha[TAM];
    Lista_Pontos ptos;
    Cada_cidade city;
    char *aux = (char*)malloc(TAM*sizeof(char));
    p = fopen("locais.txt", "r");


    while(!feof(p)){
        fgetstr(linha,sizeof(linha),p); //this function removes any '\n'.It is working,because it is used for other things

        strcpy(cidade->nome_cidade,strtok(linha,"#")); //This line is working as intended
        printf("%s\n",cidade->nome_cidade);
        strcpy(ptos->ponto.nome,strtok(NULL,"#")); //This one is not


        printf("%s\n",ptos->ponto.nome); //Printing (null)
    }
}

The file being read has the following format:

"#Coimbra"
"#Universidade De Coimbra#E uma das universidades mais antigas do mundo ainda em operaçao, sendo a mais antiga e uma das maiores do pais.#8:00h-18:00h#Segunda,Terca,Quarta,Quinta,Sexta,Sábado"

The output is supposed to be: Coimbra Universidade De Coimbra

But the actual output is just: Coimbra

  • 1
    You will want to look at [**Why is while ( !feof (file) ) always wrong?**](https://stackoverflow.com/questions/5431941/why-is-while-feoffile-always-wrong) – David C. Rankin May 18 '19 at 01:37

1 Answers1

0

The problem isn't that strtok is returning NULL. The problem is that you never initialise ptos, so it is pointing into limbo.

Also, your function seems to take an argument pt, but you never use it.

rici
  • 234,347
  • 28
  • 237
  • 341