[英]how can I fix this token comparison on input from fgets in c
我在輸入例如“exit l”時遇到問題,它適用於不正確的命令,如果我輸入 exit 它不起作用我檢查下一個令牌是否為空,但我沒有知道我是否做得對。 我想如果我檢查下一個令牌,我應該得到一個 0 或另一種檢查樣式 NULL;
while (exitloop != 1){ // do the user commands
//TODO fix for mem issue
char userinput[1000];
char delim[4] = " ";
printf("[**]Enter Command: ");
fgets(userinput, 1000, stdin);
int userinputlen = strlen(userinput);
userinput[userinputlen] = '\0';
char first_token[5];
char second_token[100];
char extra[100];
strcpy(first_token, strtok(userinput, " "));
first_token[strlen(first_token)] = '\0';
printf("%s", first_token);
if (strcmp(first_token, "exit") == 0) { // exit "done"
printf("igot in exit");
if (strtok(0,delim) == 0){ // test
perror("[-] input for exit incorrect....\n");
} else {
const char quit = 'Q';
char cmdbuff[1];
if (send(socketfd, &quit, 1, 0) < 0){
perror("[-] Error in sending request.....\n");
} else {
recv(socketfd, cmdbuff, sizeof(cmdbuff),0);
if (strcmp(&cmdbuff[0],"A") !=0) {
exitloop = 0;
perror("[-] Erorr exit failed on server side....\n");
} else {
printf("[+] Sucessful exit...\n");
exitloop = 1;
}
}
}
} else if (strcmp(first_token,"ls") == 0) { // ls
我通過在 while 循環中將 \n 替換為 \0 來解決此問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.