美文网首页
文件目录操作其他(基于itop4412)

文件目录操作其他(基于itop4412)

作者: 嵌入式工作 | 来源:发表于2018-07-13 14:16 被阅读0次

    1 删除文件目录链接

    解除链接unlink
    • man 2 unlink
    • 解除链接函数
    • int unlink(const char pathname);
    – 参数
    pathname:链接文件的路径
    – 返回值:成功返回0,错误返回-1
    – unlink指向软链接,删除软链接;指向最后一个硬链接,相当于删除文件

    2文件的拷贝

    拷贝文件
    • Linux 下并没有专门的拷贝函数和接口,需要通过open,read,
    wite 等文件操作函数实现

    3移动文件命令为mv,函数为rename

    • man 2 rename
    • int rename(const char oldpath, const char newpath)
    – 参数
    oldpath:旧的文件路径
    – 参数
    newpath:新的文件路径
    – 返回值:成功返回0,错误返回-1

    4测试代码

    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <unistd.h>
    #include <dirent.h>
    #include <stdio.h>
    
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <string.h>
    
    int main(int argv,char*argc[])
    {
        
        int fdold,fdnew,len,ret;
        char rbuf[1024];
        
        //=====================================================
        //unlink
        printf("first file is unlink file or content  yyy\n");
        if(argv<2)
        {
           printf("no unlink file err \n");
           exit(1);
           
            
        }
        ret=unlink(argc[1]);
        if(0==ret)
        {
          printf("no unlink file %s success \n",argc[1]); 
        }else
        {
            
        printf(" unlink file %s fail %d \n",argc[1],ret);
         exit(1);
           
        }
        //==============================================================
        
        
        //===============================================================
        //file move
        printf("second file is need move file or content\n");
        if(argv<3)
        {
           printf("no move file err \n");
           exit(1);
    
        }
        
        if((fdold=open(argc[2],O_RDWR))<0)
        {
          printf("open old file %s err \n",argc[2]); 
           
        }
        
        
        if((fdnew=open(argc[3],O_RDWR|O_CREAT,0777))<0)
        {
          printf("open new file %s err \n",argc[3]); 
          
        }
        
        while(1)
        {
            len=read(fdold,rbuf,1024);
            if(len)
            write(fdnew,rbuf,len);
        else
            break;
        }
        
        
        close(fdold);
        close(fdnew);
        //=========================================================================
        
        
        
        //========================================================================
        //chgname
            printf("third file is ch name file or content\n");
        if(argv<4)
        {
           printf("no chgnaem file err \n");
           exit(1);
    
        }
        if(rename(argc[4], argc[5])<0)
        {
          printf("no chg name file err \n");
           exit(1);
            
        }
        printf("no chg name file success \n");
        
        
        //======================================================================
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:文件目录操作其他(基于itop4412)

          本文链接:https://www.haomeiwen.com/subject/omdjpftx.html