日期:2014-05-16  浏览次数:20753 次

代码有错误
这个程序编译成功但是就是无法运行,运行直接提示 找不到命令是怎么回事?
C/C++ code
#include"fifo.h"

int main(int argc,char **argv)
{
  int readfifo,writefifo,dummyfd,fd;
  char *ptr,buff[MAXLINE+1],fifoname[MAXLINE];
  pid_t pid;
  ssize_t n;

  if((mkfifo(SERV_FIFO,FILE_MODE)<0)&&(errno!=EEXIST))
    printf("can't create %s",SERV_FIFO);

  readfifo=open(SERV_FIFO,O_RDONLY|O_NONBLOCK,0);
  dummyfd=open(SERV_FIFO,O_WRONLY,0);

  while((n=read(readfifo,buff,MAXLINE))>0)
    {
      if(buff[n-1]=='\n')
    n--;
      buff[n]='\0';

      if((ptr=strchr(buff,' '))==NULL)
    {
      printf("bogus request: %s",buff);
      continue;
    }

      *ptr++=0;
      pid=atol(buff);
      snprintf(fifoname,sizeof(fifoname),"/tmp/fifo.%ld",(long)pid);
      if((writefifo=open(fifoname,O_WRONLY,0))<0)
    {
      printf("cannot open:%s",fifoname);
      continue;
    }
      if((fd=open(ptr,O_RDONLY))<0)
    {
      snprintf(buff+n,sizeof(buff)-n,":can't open,%s\n",strerror(errno));
      n=strlen(ptr);
      write(writefifo,ptr,n);
      close(writefifo);
    }
      else
    {
      while((n=read(fd,buff,MAXLINE))>0)
        write(writefifo,buff,n);
      close(fd);
      close(writefifo);
    }
    }
  exit(0);
}



------解决方案--------------------
那只能说明你代码有隐藏的错误,C++编译器不认为是错误的,我以前也遇到过这种情况,调试半天才弄好呢~~
------解决方案--------------------
我试了一下,没问题。很可能是你包含的头文件有问题。


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <ctype.h>

#define MAXLINE 100
#define SERV_FIFO "server_fifo"
#define FILE_MODE 0x1ff

int main(int argc,char **argv)
{
int readfifo,writefifo,dummyfd,fd;
char *ptr,buff[MAXLINE+1],fifoname[MAXLINE];
pid_t pid;
ssize_t n;

if((mkfifo(SERV_FIFO,FILE_MODE)<0)&&(errno!=EEXIST))
printf("can't create %s",SERV_FIFO);

readfifo=open(SERV_FIFO,O_RDONLY|O_NONBLOCK,0);