使用一些posix命令的C程序

-2 c

错误:请求成员`st_mode'不是结构或联合

错误:')'标记之前的语法错误

错误:`read'的参数1的不兼容类型

/* Here is the code */

int main( int argc, char **argv)

{

    int src, dst;
    char buf[BUFSIZE];
    int n;

    if (argc!=3)
    {
      printf("\n usage: copy src dst\n");
      return -1;
    }

  struct stat src;

  if(stat(argv[1],&src) < 0)

  return 1;

  printf("File Permissions for source file: \t");

  printf("%d", S_ISDIR(src.st_mode)) ? "d" : "-");

  printf(src.st_mode & S_IRUSR) ? "r" : "-");

  printf(src.st_mode & S_IWUSR) ? "w" : "-");

  printf(src.st_mode & S_IXUSR) ? "x" : "-");


  int creat(char *dst,int perms);


  printf("File Permissions for destination file: \t");

  printf(S_ISDIR(src.st_mode)) ? "d" : "-");

  printf(dst.st_mode & S_IRUSR) ? "r" : "-");

  printf(dst.st_mode & S_IWUSR) ? "w" : "-");

  printf(dst.st_mode & S_IXUSR) ? "x" : "-");


  src=open(argv[1], O_RDONLY | S_IRUSR | S_IWUSR |S_IXUSR | );

  dst=open(argv[2], O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR |S_IXUSR);

  while ((n=read(src, buf, BUFSIZE)) > 0)

  {

    if (write(dst, buf, n) != n)

    printf("write error!");

  }

   if (n<0){

   printf("read error !");

  }

   close(src);

   close(dst);

   exit(0);

}
Run Code Online (Sandbox Code Playgroud)

J.N*_*.N. 5

编写代码的方法是错误的,在您发布的内容中存在大量语法错误(除非这是复制/粘贴效果).你写了一堆行,然后尝试编译它们.那不行.在您的学习阶段,您应该尝试每2行编译一次代码并尝试每4行运行一次(真的).

总结一下:

  • 您的大多数三元运算符... ? ... : ...都有不匹配的括号
  • 你打电话给"." int(dst)上的运算符."dot"仅适用于struct类型.
  • 你的身份完全不合适,试着改善它
  • 在另一个函数的主体中有一个函数声明,它应该在使用它的函数之前.