link unlink



  * 函数介绍:unlink()会删除参数pathname指定的文件,文件夹处理不了。成功返回0,否则返回1。unlink()会删除参数pathname指定的文件。如果该文件名为最后连接点,但有其他进程打开了此文件,则在所有关于此文件的文件描述词皆关闭后才会删除。如果参数pathname为一符号连接,则此连接会被删除。
 * 头文件:unistd.h
 */ 
 
#include <iostream> 
#include<unistd.h> 
#include<stdio.h> 
#include<string> 
 
using namespace std; 
 
int main() 

    char strPath[50] = "f:\\test.txt"; 
 
    FILE *fp = fopen(strPath,"rw"); 
    int status = access("f:\\test.txt",0);     //获取文件状态 
    if(status == 0 )                           //判断文件是否存在 
    { 
        cout << "File exists\n" << endl; 
        fclose(fp); 
        if(!unlink("f:\\test.txt"))             //删除指定文件 
        { 
            cout << "成功文件删除" << endl; 
        } 
    }else 
    { 
        cout << "No file" << endl; 
        return 0; 
    } 

 

[cpp] view plaincopy
/*
 * 函数介绍:link函数,建立硬链接,所谓的硬链接就是文件的别名,软连接相当于文件快捷方式,存放着源文件的路径
 * 头文件:unistd.h
 */ 
 
#include <iostream> 
#include<unistd.h> 
#include<stdio.h> 
#include<string> 
 
using namespace std; 
 
int main() 

    char strPath[50] = "./leeboy.txt"; 
    char chBackupFile[50] = "./leeboy_wang.txt";    //映射文件名 
 
    FILE *fp = fopen(strPath,"rw"); 
    int status = access("./leeboy.txt",0);        //获取文件状态 
    if(status == 0 )                               //判断文件是否存在 
    { 
        cout << "File leeboy exists\n" << endl; 
        fclose(fp); 
        if (link(strPath, chBackupFile) == -1)  //建立文件硬链接,相当于生成一个新的文件,但内存中放的是同一个位置 
        { 
            cout << "文件链接生成错误!"; 
            return 0; 
        } 
        if(unlink("./leeboy.txt") == -1)             //删除源文件,但备份文件还存在 
        { 
            cout << "删除源失败" << endl; 
        } 
    }else 
    { 
        cout << "No file" << endl; 
        return 0; 
    } 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值