Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.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
即使可执行文件存在,C`execlp`也会下降_C_Binary_Executable_System Calls - Fatal编程技术网

即使可执行文件存在,C`execlp`也会下降

即使可执行文件存在,C`execlp`也会下降,c,binary,executable,system-calls,C,Binary,Executable,System Calls,我是C语言的新手,我正在用Stevens&Rago的书学习UNIX。我有个问题。早期的数据之一是我的实现中没有工作 #include "apue.h" #include <sys/wait.h> int main(void)

我是C语言的新手,我正在用Stevens&Rago的书学习UNIX。我有个问题。早期的数据之一是我的实现中没有工作

#include "apue.h"                                     
#include <sys/wait.h>                                 

int                                                   
main(void)                                            
{                                                     
  char buf[MAXLINE];                                  
  pid_t pid;                                          
  int status;                                         

  printf("%% ");                                      
  while (fgets(buf, MAXLINE, stdin) != NULL) {        
    if (buf[strlen(buf) - 1] == "\n")                 
      buf[strlen(buf) - 1] = 0;                       

    if ((pid = fork()) < 0) {                         
      err_sys("Fork error: %d");                      
    }                                                 
    else if ((pid == 0) && (access(buf, F_OK ) != 1)){ /* Here I have an extra check to ensure, that the binary exists. */
      execlp(buf, buf, (char *)0);                    
      err_ret("Couldn't execute: %s", buf);           
      exit(127);                                      
    }                                                 

    if ((pid = waitpid(pid, &status, 0)) < 0)         
      err_sys("waitpid error");                       
    printf("%% ");                                    
  }                                                   
  exit(0);                                            
}                                                     
我有几个关于execlp(buf,buf,(char*)0)的问题

为什么
execlp
不能执行二进制文件

int execlp(常量字符*文件,常量字符*参数,…)

为什么
buf
通过了两次?If第二个参数必须是二进制的参数。 我找不到,它是什么意思:
(char*)0
?这是什么指针?那里面的零呢

Couldn't execute: /bin/ls
: No such file or directory
请注意,错误消息位于新行上,但您试图从输入中删除行尾字符。因此,您的删除似乎不起作用

原因是:

if (buf[strlen(buf) - 1] == "\n")
您正在比较字符(
buf[x]
)和字符串(
“\n”
),即指针。打开编译器的警告并仔细阅读

请尝试以下方法:

if (buf[strlen(buf) - 1] == '\n')
execlp
的参数描述如下:

按照惯例,第一个参数应该指向与正在执行的文件关联的文件名

或者在POSIX中


(char*)0
是类型为
char*
的空指针。这里没有什么神奇的事情,值为0的整型常量是一个空指针常量。但由于
execlp
是一个可变函数,因此强制转换是必要的-常量零将被视为
int
,否则,它可能与指针大小不同(空指针的内部表示可能与整数0的内部表示不同),导致未定义的行为。

您的代码是正确的,我只是编译了一下。不要用双引号(“),只要用单引号就可以了

buf[strlen(buf) - 1] == '\n'
也包括这个

#include<stdio.h>
#include<stdlib.h>

#include<unistd.h>

#include<string.h>
#包括
#包括
#包括
#包括

如果你不能识别空指针和类型转换,那么现在还不是你处理流程管理之类的事情的时候。先学习C语言的基础知识。
#include<stdio.h>
#include<stdlib.h>

#include<unistd.h>

#include<string.h>