0

标题非常不言自明,我想知道在找到某个字符时停止打印 csv 文件的最佳方法是什么,例如,我想在找到“;”时停止打印文本,这是我的尝试:

void testeperguntas(){
    FILE *in;

    char str[MAXCHARS];
    if ((in = fopen("../data/perguntas.txt", "r")) == NULL) {
        printf("Erro: perguntas!");
    }
    if (in) {
        while (fscanf(in, "%s", str)!=EOF)
            printf("%s",str);
        fclose(in);
    }
}

和:

void testeperguntas1(){

    char buf[400];

    FILE *in = fopen("../data/perguntas.txt", "r");


    while (fgets(buf, sizeof(buf), in))
    {
        char *token;

        token = strtok(buf, ",");
        while (token!= NULL) {
            printf("%s", token);
            token = strtok(NULL, ",");
        }
        printf("\n");
    }

}

两种方式我都找不到方法,我会很感激任何帮助 thx

4

1 回答 1

0

我在你的循环中找不到break 关键字

while (fscanf(in, "%s", str)!=EOF)
{
     printf("%s",str);
    if(strcmp(str,";")==0)
       break;

}

像这样

于 2020-12-18T12:42:47.763 回答