我怎样才能修复这个标记比较来自 c 中 fgets 的输入
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] = '[=10=]';
char first_token[5];
char second_token[100];
char extra[100];
strcpy(first_token, strtok(userinput, " "));
first_token[strlen(first_token)] = '[=10=]';
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 循环中用 \0 替换 \n 解决了这个问题
我在输入例如“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] = '[=10=]';
char first_token[5];
char second_token[100];
char extra[100];
strcpy(first_token, strtok(userinput, " "));
first_token[strlen(first_token)] = '[=10=]';
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 循环中用 \0 替换 \n 解决了这个问题