uc_day05
2014-03-03 22:13
316 查看
一,stat函数
#include<sys/stat.h>
使用这个函数可以获取文件的属性。
int stat(const char *path,struct stat* buf);
第一个参数是输入参数,第二个是输出参数
成功返回0,失败返回1
man -s2 stat
stat.c
#include<stdio.h>
#include<sys/stat.h>
#include<unistd.h>
#include<stdlib.h>
int main(){
//获取一个文件的属性
struct stat s = {};
int res = stat("a.txt",&s);
if(res == -1){
perror("获取文件属性失败");
exit(-1);
}else{
printf("获取文件属性成功");
}
printf("i_node number:%d\n",s.st_ino);
printf("mode:%d\n",s.st_mode);
printf("硬连接数:%d\n",s.st_nlink);
printf("文件属主UID:%d\n",s.st_uid);
return 0;
}
二,fstat函数
int fstat(int filedes,struct stat* buf);
使用这个函数,需要先打开文件
三,lstat函数
int lstat(const char *path,struct stat* buf);
主要用于获取软连接文件
#include<stdio.h>
#include<sys/stat.h>
#include<stdlib.h>
#include<unixstd.h>
int main(){
struct stat s;
stat("a.txt.sln",&s);//获得的是a.txt的属性
printf("size:%d\n",s.st_size);
lstat("a.txt.sln",&s);//获取到的是a.txt.sln的属性
printf("size:%d\n",s.st_size);
return 0;
}
//软连接是快捷方式,对软连接操作,等于对源文件的操作
四,access函数,测试文件是否具有某权限
五,权限屏蔽字
int main(){
mode_t old = umask(0123);//本人无执行,同组无写,其他无执行和写
int fd = open("abc",O_RDWR|O_CREAT,0666);
umask(old);//恢复以前的权限屏蔽字
return 0;
}
六,把虚拟地址映射到文件上
#include<stdio.h>
#include<sys/mman.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/types.h>
#include<stdlib.h>
#include<string.h>
struct Emp{
int id;
char name[10];
int gender;
double salary;
};
int main(){
int fd = open("emp.dat",O_RDWR|O_CREAT|O_TRUNC,0666);
//映射文件之前,要确保文件的大小一定够用
ftruncate(fd,sizeof(struct Emp)*3);
if(fd == -1){}
void* p = res = mmap(0,sizeof(struct Emp)*3,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
if(p == MAP_FAILED){失败}
struct Emp* pe = p;
pe[0].id = 100;
strcpy(pe[0].name,"Daniel");
pe[0].gender = 1;
pe[0].salary = 23434;
.......
.......
munmap(p,sizeof(struct Emp)*3);
return 0;
}
//映射可以是私有映射,私有映射是写到文件缓冲区里面了
//映射进去只有自己的程序可以读取,另外的程序不能读取
//而共享映射不存在这个问题
//而映射到内存私有和共享无所谓,因为都只有自己看到
七,readlink
用这个函数才可以读取到软连接的真正内容,也就是源文件的内容
八,目录操作
mkdir
rmdir//只能删除空目录,没有删除非空目录的函数
getcwd//获取当前目录
#include<sys/stat.h>
使用这个函数可以获取文件的属性。
int stat(const char *path,struct stat* buf);
第一个参数是输入参数,第二个是输出参数
成功返回0,失败返回1
man -s2 stat
stat.c
#include<stdio.h>
#include<sys/stat.h>
#include<unistd.h>
#include<stdlib.h>
int main(){
//获取一个文件的属性
struct stat s = {};
int res = stat("a.txt",&s);
if(res == -1){
perror("获取文件属性失败");
exit(-1);
}else{
printf("获取文件属性成功");
}
printf("i_node number:%d\n",s.st_ino);
printf("mode:%d\n",s.st_mode);
printf("硬连接数:%d\n",s.st_nlink);
printf("文件属主UID:%d\n",s.st_uid);
return 0;
}
二,fstat函数
int fstat(int filedes,struct stat* buf);
使用这个函数,需要先打开文件
三,lstat函数
int lstat(const char *path,struct stat* buf);
主要用于获取软连接文件
#include<stdio.h>
#include<sys/stat.h>
#include<stdlib.h>
#include<unixstd.h>
int main(){
struct stat s;
stat("a.txt.sln",&s);//获得的是a.txt的属性
printf("size:%d\n",s.st_size);
lstat("a.txt.sln",&s);//获取到的是a.txt.sln的属性
printf("size:%d\n",s.st_size);
return 0;
}
//软连接是快捷方式,对软连接操作,等于对源文件的操作
四,access函数,测试文件是否具有某权限
五,权限屏蔽字
int main(){
mode_t old = umask(0123);//本人无执行,同组无写,其他无执行和写
int fd = open("abc",O_RDWR|O_CREAT,0666);
umask(old);//恢复以前的权限屏蔽字
return 0;
}
六,把虚拟地址映射到文件上
#include<stdio.h>
#include<sys/mman.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/types.h>
#include<stdlib.h>
#include<string.h>
struct Emp{
int id;
char name[10];
int gender;
double salary;
};
int main(){
int fd = open("emp.dat",O_RDWR|O_CREAT|O_TRUNC,0666);
//映射文件之前,要确保文件的大小一定够用
ftruncate(fd,sizeof(struct Emp)*3);
if(fd == -1){}
void* p = res = mmap(0,sizeof(struct Emp)*3,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
if(p == MAP_FAILED){失败}
struct Emp* pe = p;
pe[0].id = 100;
strcpy(pe[0].name,"Daniel");
pe[0].gender = 1;
pe[0].salary = 23434;
.......
.......
munmap(p,sizeof(struct Emp)*3);
return 0;
}
//映射可以是私有映射,私有映射是写到文件缓冲区里面了
//映射进去只有自己的程序可以读取,另外的程序不能读取
//而共享映射不存在这个问题
//而映射到内存私有和共享无所谓,因为都只有自己看到
七,readlink
用这个函数才可以读取到软连接的真正内容,也就是源文件的内容
八,目录操作
mkdir
rmdir//只能删除空目录,没有删除非空目录的函数
getcwd//获取当前目录
相关文章推荐
- JNI使用:步步解析
- k近邻法
- 2.3 UIButton
- mysql的存储引擎
- 在Spring框架下配置JPA
- 【面试】二进制妙用
- CentOS 6.4安装配置LNMP服务器(Nginx+PHP+MySQL)
- 理想和诱惑
- PL SQL Developer 使用总结
- 3.3 周一 数电
- Servlet的配置和doget()的输出
- JDBC中的PreparedStatement相比Statement的好处有哪些?
- Java 多线程实现方法
- notify 通知
- 《深入理解Android:Wi-Fi,NFC和GPS》章节连载[节选]--第二章 深入理解Netd
- java:N的N次方
- 捡贝壳的小男孩
- 负载均衡器部署方式和工作原理
- Granola: Low-Overhead Distributed Transaction Coordination
- Adobe Flash Player11.3安装不了,提示不是最新版本