Strtok在第一个令牌之后返回(空)

时间:2019-05-17 22:54:17

标签: c strtok

我正在尝试使用标记“#”分隔从文件读取的字符串。 StrTok正确地分隔了第一个令牌,但是当我打印应该是第二个令牌的东西时,它只会返回(空)。

我试图更改正在读取的文件的格式,并使用aux变量保存strtok(NULL,“#”)的内容,但是结果没有改变。

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)
    }
}

正在读取的文件具有以下格式:

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

输出应该是: 科英布拉 科英布拉大学

但是实际输出仅为: 科英布拉

1 个答案:

答案 0 :(得分:0)

问题不是strtok返回NULL。问题在于您永远不会初始化ptos,因此它指向了困境。

此外,您的函数似乎带有参数pt,但是您从不使用它。

相关问题