Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
do While循环不适用于C中的字符串比较_C - Fatal编程技术网

do While循环不适用于C中的字符串比较

do While循环不适用于C中的字符串比较,c,C,我对C语言编程非常陌生,所以请容忍我。下面是我的C语言代码。据我所知,它应该起作用,但当我进入出口时,根据我的逻辑,它应该起作用。事实并非如此,请告诉我我做错了什么 #include <stdio.h> #include <unistd.h> #include <string.h> #include <stdlib.h> #include <stdbool.h> int main () { char command[50]; do

我对C语言编程非常陌生,所以请容忍我。下面是我的C语言代码。据我所知,它应该起作用,但当我进入出口时,根据我的逻辑,它应该起作用。事实并非如此,请告诉我我做错了什么

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h> 
#include <stdbool.h>

int main () {
char command[50];

do{
    int  pid ;
    char bin_dir[100]     ;
    char sbin_dir[100]    ;
    command[50] = '\0';    

    printf("\n get input to execute command: ");
    fgets (command, 100, stdin);

    printf("\n Entered command is : %s " ,command);

    strcpy(sbin_dir,"/sbin/");
    strcpy(bin_dir,"/bin/");

    strcat(bin_dir  ,command);
    strcat(sbin_dir ,command);

     if( access( bin_dir, F_OK ) != -1 ) {
         printf(" command found in bin Directory \n");
       } else if (access( sbin_dir, F_OK ) != -1 ) {
         printf(" command found in sbin Directory \n");
       }else {
         printf("command not found \n");
       }

    }while(strcmp (command, "exit") == 0);
   return 0;
}
循环必须在strcmp…!=0,而不是==0。 我认为fgets将在末尾读取带有LF的行-比较strcmp命令,退出\n。 PS:命令[50]='\0'错误。必须是command[49]=0或更好的memsetcommand、0、sizeof命令


PS2:fgets命令,100,stdin没有什么问题-命令是50字节的数组,fgets最多允许100个字节。使用fgets命令、sizeof命令、stdin。

此站点不是调试器。请学习使用一个,和/或将其简化为.fgets命令,100,stdin;char命令[50];->fgets命令,sizeof命令,stdin;并删除换行符。亲爱的,在进入退出后仍无法退出循环-已尝试两个strcmp命令,退出\n!=0和strcmp命令,退出!=0@ImranNaqvi将断点放在FGET之后,并查看命令缓冲区中的值。