gpt4 book ai didi

c - Strtok 在第一个标记后返回 (null)

转载 作者:行者123 更新时间:2023-11-30 16:17:45 25 4
gpt4 key购买 nike

我试图用标记“#”分隔从文件中读取的字符串。 StrTok 正确地分隔了第一个标记,但是当我打印应该是第二个标记的内容时,它只返回 (null)。

我尝试更改正在读取的文件的格式并使用 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)
}
}

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

“#科英布拉”
“#科英布拉大学#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"

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

但实际输出只是:科英布拉

最佳答案

问题不在于 strtok 返回 NULL。问题是您从未初始化过 ptos,因此它指向了 hell 。

此外,您的函数似乎采用参数 pt,但您从未使用过它。

关于c - Strtok 在第一个标记后返回 (null),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56194463/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com