linux C判断文件是否存在【转】
转自:http://blog.csdn.net/kingjo002/article/details/8442146
一、access函数
功能描述:
检查调用进程是否可以对指定的文件执行某种操作。 用法:
#include <unistd.h>
#include <fcntl.h> int access(const char *pathname, int mode); 参数:
pathname: 需要测试的文件路径名。
mode: 需要测试的操作模式,可能值是一个或多个R_OK(可读?), W_OK(可写?), X_OK(可执行?) 或 F_OK(文件存在?)组合体。 返回说明:
成功执行时,返回0。失败返回-,errno被设为以下的某个值
EINVAL: 模式值无效
EACCES: 文件或路径名中包含的目录不可访问
ELOOP : 解释路径名过程中存在太多的符号连接
ENAMETOOLONG:路径名太长
ENOENT: 路径名中的目录不存在或是无效的符号连接
ENOTDIR: 路径名中当作目录的组件并非目录
EROFS: 文件系统只读
EFAULT: 路径名指向可访问的空间外
EIO: 输入输出错误
ENOMEM: 不能获取足够的内核内存
ETXTBSY:对程序写入出错 #include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h> int main()
{
if((access("test.c",F_OK))!=-)
{
printf("文件 test.c 存在.\n");
}
else
{
printf("test.c 不存在!\n");
} if(access("test.c",R_OK)!=-)
{
printf("test.c 有可读权限\n");
}
else
{
printf("test.c 不可读.\n");
} if(access("test.c",W_OK)!=-)
{
printf("test.c 有可写权限\n");
}
else
{
printf("test.c 不可写.\n");
}
if(access("test.c",X_OK)!=-)
{
printf("test.c 有可执行权限\n");
}
else
{
printf("test.c 不可执行.\n");
} return ;
} #include <stdio.h>
#include <time.h>
int main()
{
time_t now = time(NULL);
char buf[];
strftime(buf,,"%Y%m%d",localtime(&now));
printf("%s\n",buf); strftime(buf,,"%Y-%m-%d %H:%M:%S",localtime(&now));
printf("%s\n",buf); strftime(buf,,"%y%m%d %H:%M:%S",localtime(&now));
printf("%s\n",buf); strftime(buf,,"%y%m%d",localtime(&now));
printf("%s\n",buf); strftime(buf,,"%H:%M:%S",localtime(&now));
printf("%s\n",buf);
return ;
}
linux C判断文件是否存在【转】的更多相关文章
- linux shell判断文件,目录是否存在或者具有权限
在linux中判断文件,目录是否存在或则具有的权限,根据最近的学习以及网上的资料,进行了以下的总结: #!/bin/sh myPath="/var/log/httpd/" myFi ...
- Linux shell判断文件和文件夹是否存在
shell判断文件,目录是否存在或者具有权限 #!/bin/sh myPath="/var/log/httpd/" myFile="/var /log/httpd/acc ...
- [转] Linux shell判断文件和文件夹是否存在
shell判断文件,目录是否存在或者具有权限 #!/bin/sh myPath="/var/log/httpd/" myFile="/var /log/httpd/acc ...
- linux下判断文件和目录是否存在[总结]
1.前言 工作中涉及到文件系统,有时候需要判断文件和目录是否存在.我结合APUE第四章文件和目录,总结一下如何正确判断文件和目录是否存在,方便以后查询. 2.stat系列函数 stat函数用来返回与文 ...
- linux下判断文件和目录是否存在
1.前言 工作中涉及到文件系统,有时候需要判断文件和目录是否存在.我结合APUE第四章文件和目录,总结一下如何正确判断文件和目录是否存在,方便以后查询. 2.stat系列函数 stat函数用来返回与文 ...
- linux shell 判断文件是否存在等符号
-a file exists. -b file exists and is a block special file. -c file exists and is a character spec ...
- Linux shell判断文件和文件夹是否存在(转发)
#!/bin/sh myPath="/var/log/httpd/" myFile="/var /log/httpd/access.log" #这里的-x 参数 ...
- linux C判断文件是否存在
access函数 功能描述: 检查调用进程是否可以对指定的文件执行某种操作. 用法: #include <unistd.h> #include <fcntl.h> int ...
- linux c 判断文件存在,遍历文件,随机修改文件内容
#include<stdio.h> #include<stdlib.h> #include<time.h> #include<assert.h> #in ...
随机推荐
- 2.TCP_IP互联线缆_TCP_UDP报文抓包详解
TCP_IP互联线缆_TCP_UDP报文抓包详解 2.1网线标准 直通线 交叉线 异种设备互联使用直通线 同种设备互联使用交叉线 TCP和UDP 端口寻址 TCP数据格式 TCP三次握手 UDP数据格 ...
- 楼市、股市后下一届ZF将用什么去做超发货币的蓄水池(
这是天涯论坛上最近的的一个热帖合集,周小川曾直言超发货币需要蓄水池来装,以控制通货膨胀.这个蓄水池楼市做过.股市曾经也充当过,或许现在的地下钱庄也算一部分吧,那下一届政府会如何选择这个蓄水池呢,这或将 ...
- Python之路-python环境安装和简单的语法使用
一.环境安装 Win10(安装python3.x): 1.下载安装包 https://www.python.org/downloads/ 2.安装完成后设置环境变量. 桌面-“计算机”-右 ...
- Swagger简介
前言 Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件.本文简单介绍了在项目中集成swagger的方法和一些常见问题.如果想深入分析项目源码,了解更多内容,见参考资料. S ...
- ajax get和post请求 后端接收并返回数据
get请求$(function(){ //alert("23"); var x = "#page"; var y = "${ctx!}/static/ ...
- OSPF理解
from http://kingdee.blog.51cto.com/98119/27310STP,PIM,OSPF,长的好像(*_*)可以把整个网络(一个自治系统AS)看成一个王国,这个王国可以分成 ...
- 刷新本地的DNS缓存
用“WIN +R”快捷键打开运行窗口,输入“cmd”命令,进行命令行窗口.
- 多语言本地化开发Localized
NSString * ViewsLocalizedStringFormat(NSString *key,NSString *defValue); // Handle localized strings ...
- PostgreSQL/bin
pg_receivexlog pg_receivexlog—以流的方式从一个PostgreSQL集簇得到事务日志 pg_receivexlog被用来从一个运行着的PostgreSQL集簇以流的方式得到 ...
- Leetcode: Water and Jug Problem && Summary: GCD求法(辗转相除法 or Euclidean algorithm)
You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...