美文网首页
文件目录操作其他(基于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)

    1 删除文件目录链接 解除链接unlink• man 2 unlink• 解除链接函数• int unlink(c...

  • 文件和目录处理相关

    文件和目录处理相关 题: 考点:文件操作/写入操作; 延伸:目录操作函数,其他文件操作; 文件读写操作 文件系统函...

  • Mac 终端常用命令

    目录操作 文件操作 其他命令 附:我的博客地址

  • Mac终端常用的基本命令

    目录操作: 文件操作: 选择操作: 进程操作: 时间操作: 网络与通信操作: Korn Shell 命令 : 其他...

  • ubuntu 文件管理命令

    目录管理命令 文件操作命令 文件属性查看以及详解 d :目录(其他的字符有,-普通文件、l:链接文件、b:设备文件...

  • 08.文件和目录常用命令

    目标 查看目录内容 切换目录 创建和删除操作 拷贝和移动文件 查看文件内容 其他 重定向 管道 01. 查看目录内...

  • 测试人员必备linux知识(3)

    前面我们介绍了文件查看,编辑的相关操作,本节介绍其他常见的文件操作。 mkdir 创建目录 例子:mkdir te...

  • LInux-基本指令

    1.基本文件和目录的操作 2.系统,进程,网络 系统操作 进程操作 网络操作 3.其他工具 日期

  • Linux文件操作

    文件操作 (Linux文件操作)) [文件|目录] Linux文件操作:为了对文件和目录进程处理,你需要用到系统...

  • CentOS安装nginx,启动后403

    基于root用户权限操作 nginx 启动,重启,停止:进入 nginx/sbin 目录配置文件测试:./ngin...

网友评论

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

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