文件处理常用方法及link和unlink讲解
来源:互联网 发布:男人看女人第一眼 知乎 编辑:程序博客网 时间:2024/06/05 18:34
/* * 函数介绍:access函数,当对文件使用时,判断是否存在指定的文件,以及是否能够按指定的模式进行访问。 * 头文件:unistd.h * 参数mode可为以下的其中之一: * 00 只存在 * 02 写权限 * 04 读权限 * 06 读和写权限 * 返回值:如果文件拥有给定的模式则返回0,如果发生错误返回-1。 * 函数介绍: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; }}
/* * 函数介绍: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; }}
3、获取文件大小
/* * 函数介绍:ftell获取文件指针位置,可以获取文件大小 * 头文件:stdio.h */#include <iostream>#include <stdio.h>using namespace std;int main(){ FILE *fp = fopen("f:\\lee.txt","rw"); //打开文件 fseek(fp,0,SEEK_END); //设置的文件末尾 int len = ftell(fp); //获取指针位置,从而获取文件大小 cout << "文件大小:" << len << endl;}
欢迎网友帮忙添加,写在回复里,我来整理
- 文件处理常用方法及link和unlink讲解
- link和unlink讲解
- unlink,link,symlink等,处理文件和目录
- 文件与目录操作--link、unlink、remove和rename函数 及 文件与目录--utime函数
- link() 和 unlink()函数示例
- link、unlink、remove和rename
- link unlink
- Linux中link,unlink
- 4.15 link、unlink函数-创建和删除硬链接
- 实验 6-1 1.针对符号连接文件和硬连接文件。测试以下函数,分析其执行过程并给出结论。 link() unlink() symlink() readlink() stat() lstat(
- 从symbol link和hard link 到 unlink函数的一点记录
- unix环境高级编程-4.15-link,unlink,remove和rename函数
- php文件删除,unlink()
- 抽象工厂方法java实现代码及常用范围讲解
- VC常见LINK处理方法
- VC常见LINK处理方法
- VC常见LINK处理方法
- Linux中link,unlink,close,fclose详解
- CMD命令行高级教程精选合编
- SQLite 3.7.13的加密解密(四)—— 挂接加密解密函数
- 设计模式之二 --- Singleton 模式 (java)
- Android小知识-1
- CheckStyle使用详细手册
- 文件处理常用方法及link和unlink讲解
- DataStage简介
- pythonchallenge--10
- SQLite 3.7.13的加密解密(五)—— 修正编译错误和警告
- HDU 1018 Big Number
- 数据量大时,页面延时加载方法.
- Androidz之返回键退出程序
- Java什么会成功
- SQL 中不同类型的表连接