Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.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
构建Linux cp命令的版本_C_Linux_Cp - Fatal编程技术网

构建Linux cp命令的版本

构建Linux cp命令的版本,c,linux,cp,C,Linux,Cp,您好,我正在尝试用C语言构建一个linux cp命令版本,它可以处理以下测试用例 cpy f1-将f1的内容复制到f2 cpy f d-将f复制到d/f cpy d1/d2/d3/f1 f2-将f1的内容复制到f2 cpy d1/d2/d3/f d-副本f至d/f cpy d1/d2/d3/f d4/d5/d6/f d-将文件复制到目录d 我已经得到了Copyfile函数的代码,我所要做的就是创建我的主函数来调用Copyfile并验证测试用例是否正确处理。下面是CopyFile函数和main函数

您好,我正在尝试用C语言构建一个linux cp命令版本,它可以处理以下测试用例

  • cpy f1-将f1的内容复制到f2
  • cpy f d-将f复制到d/f
  • cpy d1/d2/d3/f1 f2-将f1的内容复制到f2
  • cpy d1/d2/d3/f d-副本f至d/f
  • cpy d1/d2/d3/f d4/d5/d6/f d-将文件复制到目录d
  • 我已经得到了Copyfile函数的代码,我所要做的就是创建我的主函数来调用Copyfile并验证测试用例是否正确处理。下面是CopyFile函数和main函数。我走对了吗?我无法进行测试,因为我目前在编译时出错(错误:确认Copyfile的类型),而且我似乎无法找出错误所在

    int copyFiles(int argC, char* argV[]){
       int srcFd;
       int dstFd;
       int charCnt;
       int buffersize = strtol(argV[3], NULL, 10);
       char buf[buffersize];
    
       /*Check args*/
       if( argC!=4 ){
          fprintf( stderr, "usage: %s source destination\n", argV[0]);
          exit(1);
       }
    
       /*Open the files*/
       srcFd= open(argV[1],O_RDONLY);
       if( srcFd==-1 ){
          errExit("Cannot open ", argV[1]);
       }
       dstFd= creat(argV[2],COPYMODE);
       if( dstFd==-1 ){
            errExit( "Cannot create ", argV[2]);
       }
    
       /*Copy the data*/
       while( (charCnt= read(srcFd,buf,buffersize)) > 0 ){
           if( write(dstFd,buf,charCnt ) != charCnt ){
              errExit("Write error to ", argV[2]);
           }
        }
       if( charCnt==-1 ){
          errExit("Read error from ", argV[1]);
       }
    
       /*Close files*/
       if ( close(srcFd) == -1 || close(dstFd) == -1 ){
          errExit("Error closing files","");
       }
    
    }

    int-copyFiles(char*srcFd,char*dstFd);
    int main(int argC,char*argV[])
    {
    char*srcFd=argV[1];
    char*dstFd=argV[2];
    如果(srcFd[0]!='/'和&dstFd[0]!='/')//cpy f1
    {
    复制文件(srcFd、dstFd);
    }
    否则如果(srcFd[0]!='/'&&dstFd[0]=='/')///cpy f1/d
    {
    int i;
    
    对于(i=1;i您已将参数定义为int,char*为
    int-copyFiles(int-argC,char*argV[])

    但是在主程序中声明为char*和char*
    int-copyFiles(char*srcFd,char*dstFd);


    这是为什么它会显示错误。

    原型
    int-copyFiles(char*srcFd,char*dstFd);
    与函数签名
    int-copyFiles(int-argC,char*argV[]){…}不匹配
    。它们必须是相同的。事实上,您根本不需要原型,只需将其删除即可。在测试用例1-5中,目录是绝对路径还是相对路径?在测试用例中,它似乎是相对路径。但代码对绝对路径进行了解析。
     int copyFiles(char *srcFd,char *dstFd);
    
     int main(int argC, char* argV[])
    {
       char *srcFd = argV[1];
       char *dstFd = argV[2];
    
        if( srcFd[0] != '/' && dstFd[0] != '/' )//cpy f1 f2
        {
          copyFiles(srcFd, dstFd);
        }
        else if( srcFd[0] != '/' && dstFd[0] == '/' )//cpy f1 /d 
        {
          int i;
           for(i=1; i<=strlen(dstFd); i++)
           {
              dstFd[(i-1)] = dstFd[i];
           }
              strcat(dstFd, "/");
              strcat(dstFd, srcFd);
    
              copyFiles(srcFd, dstFd);
    
        } 
          else
          {
               fprintf(stderr, "usage: cp1 source destination\n");
                exit(1);
      }
    }