簡體   English   中英

解決 strtok While 循環中斷外循環問題

[英]solve strtok While loop breaks outer while loop problem

如果我刪除 strtok 的 while 循環,則外部 while 循環可以繼續,直到我鍵入 exit。 但是外部的 while 循環中斷,內部的 strtok 循環中斷。 我想知道為什么會發生這種情況。

#include <stdio.h>
#include <stdlib.h> // For exit() function
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <memory.h>
#include <stdlib.h>

int main()
{
    char input[1024];
    fgets(input,1024,stdin);
    do
    {
        printf("%s\n",input);
        char* token = strtok(input, " ");
        while (token) {
            printf("%s\n", token);
            token = strtok(NULL, " ");
        }
        fgets(input,1024,stdin);
    }while (strcmp(input, "exit\n") == 1);
    return 0;
}

而不是這些陳述

    fgets(input,1024,stdin);
}while (strcmp(input, "exit\n") == 1)

寫出以下內容

}while ( fgets(input,1024,stdin) != NULL && strcmp(input, "exit\n") != 0 );

如果兩個字符串不相等,函數strcmp可以返回任何非零值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM