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 ...
随机推荐
- dataguard集群搭建
dataguard集群搭建 1. 创建虚拟机 创建一台虚拟机配置如下: 系统Red Hat Enterprise 6(64位).16vCPU.8G内存.两块VM Network类型网卡.三块硬盘分别为 ...
- 【转载】:【C++跨平台系列】解决STL的max()与numeric_limits::max()和VC6 min/max 宏冲突问题
http://www.cnblogs.com/cvbnm/articles/1947743.html 多年以前,Microsoft 幹了一件比 #define N 3 還要蠢的蠢事,那就是在 < ...
- Perl Debug error: SetConsoleMode failed, LastError=|6|
Windows Strawberry Perl. 解决办法: 1. 设置环境变量 TERM = dumb 2. 重启 CMD 参考资料: http://padre.perlide.org/trac/t ...
- iOS 自定义UITabBarController的tabBar
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDeleg ...
- PL/SQL Developer连接到Oracle 12c
安装32位的Oracle客户端instantclient-basic-nt-12.1.0.1.0.zip 下载地址:http://www.oracle.com/technetwork/topics/w ...
- SqlServer跨库查询
由于业务的拆分,数据库拆分为两种作用: 汇总数据库(Master,头节点数据库), 子节点数据库(Compute Node,计算子节点数据库) 这样,就设计到子节点访问头节点数据库中的某张汇总表,这种 ...
- views of postgresql user password and encrypted or unencrypted
password_encryption = onpostgres=# create user user1 with encrypted password 'user1';CREATE ROLEpost ...
- How to debug PostgreSQL function with pgAdminIII
How to debug plpgsql with pgAdminIII [root@localhost soft_bak]# git clone git://git.postgresql.org/g ...
- eclipse 导入jdbc4.jar 包
详细讲解链接 http://wenku.baidu.com/link?url=QUhO2rIL2fYRgOUyd1TQPEgbl0jQr156ioxK5fiwSPm_Tset2okpBEJcO1fmz ...
- C#控件:TabControl
tabcontrol在切换页面的时候经常用到 这里讲下如何让tabcontrol更好看 ref:http://blog.csdn.net/conmajia/article/details/759671 ...