C 高级编程5 IO与文件权限
三.基于文件的描术符号
.得到文件描术符号/释入文件描术符号
a.文件类型
目录文件 d
普通文件 f
字符设务文件 c
块设备文件 b
软连接文件 l
管道文件 p
socket文件 s 字符设备文件:
[root@monitor ~]# ls -l /dev/console
crw------- root root , May : /dev/console 块设备文件 :
[root@monitor ~]# ls -l /dev/xvda1
brw-rw---- root disk , May : /dev/xvda1 管道文件:
mkfifo p.pipe
[root@monitor ~]# ll p.pipe
prw-r--r-- root root May : p.pipe socket文件
[root@monitor ~]# ll /var/lib/mysql/mysql.sock
srwxrwxrwx mysql mysql Apr : /var/lib/mysql/mysql.sock b.文件的属性
.属性的表达方式:绝对模式,字符模式
.文件的权限属性:
读
写
执行
粘附位权限
用户设置位权限 特殊权限 OWNER group 其他用户 chmod p.pipe
p--------- root root May : p.pipe
[root@monitor ~]# chmod p.pipe
[root@monitor ~]# ll p.pipe
p--------T root root May : p.pipe
[root@monitor ~]# chmod p.pipe
[root@monitor ~]# ll p.pipe
p--------t root root May : p.pipe
[root@monitor ~]# chmod p.pipe
[root@monitor ~]# ll p.pipe
p-----S--- root root May : p.pipe
[root@monitor ~]# chmod p.pipe
[root@monitor ~]# ll p.pipe
p-----s--- root root May : p.pipe
[root@monitor ~]# chmod p.pipe
[root@monitor ~]# ll p.pipe
p--S------ root root May : p.pipe
[root@monitor ~]# ll p.pipe
p--S------ root root May : p.pipe
[root@monitor ~]# chmod p.pipe
[root@monitor ~]# ll p.pipe
p--s------ root root May : p.pipe
[root@monitor ~]# chmod a+s p.pipe
[root@monitor ~]# ll p.pipe
p--s--S--- root root May : p.pipe
[root@monitor ~]# chmod a+st p.pipe
[root@monitor ~]# ll p.pipe
p--s--S--T root root May : p.pipe s:S:t:T:
1.1. s设置位
:组设置位
:用户设置位
s对执行有效
无效的设置位使用S表示
设置位:向其他用户开放拥有者权限的权限:用户设置位
向其他用户开放组有户权限的权限:组用户设置位
设置位只对执行程序有意义(执行权限有意义) 程序在执行的时候到底拥有的是 执行者 用户权限,还是 文件拥有者 的权限????
有设置位:文件拥有者的权限,程序执行过程中
无设置位:执行者用户权限,程序执行过程中 程序执行有两个用户: 实际用户:标示进程到底是谁
有效用户:标示进程访问资源的权限
上述一般情况是一样的,有时候被setUID改变 总结:
沾附位的作用: 防止其他有写权限用户删除文件
设置位的作用: 向其他执行者开发组或者用户的权限 2.1. t设置位
表示沾附位设置
t对写文件有意义
无效的沾附位使用T表示. 沾附的目的:防止有些权限的用户删除文件
eg:
cat /etc/shadow
cat /etc/password
password
eg:
赵: main rwx a.txt wr
张三:main x a.txt r
张三可以执行main,但对a.txt 只读权限?? 练习:
.使用cat创建一个文件
.设置沾附位,并观察属性
.设置用户设置位, 并观察属性
.设置组设置位, 并观察属性
.考虑w权限与沾附位的关系
.考虑x权限与设置位的关系. .通过文件描述符号读写各种数据.
open函数与creat函数 int open(
const char *filename,//文件名
int flags,//open的方式[创建/打开]
mode_t mode//权限(只有创建的时候有效)
)
返回:
>=:内核文件描述符号.
=-:打开/创建失败 open的方式:
必选方式:O_RDONLY O_WRONLY O_RDWR,必须选择一个
创建/打开:O_CREAT
可选方式:
对打开可选方式:O_APPEND O_TRUNC(清空数据)
对创建可选方式:O_EXCL
组合:
创建:
O_RDWR|O_CREAT
O_RDWR|O_CREAT | O_EXCL 打开:
O_RDWR
O_RDWR|O_APPEND
O_RDWR|O_TRUNC
权限:
建议使用8进制数
关闭
void close(int fd); 案例1:
创建文件
案例2:
创建文件并写入数据
short float
tom 99.99
bush 65.00
达内 100.00
注意:
文件的创建的权限受系统的权限屏蔽的影响
umask //显示屏蔽权限.
umask //设置权限屏蔽. ulimit -a 显示所有的其他限制. /*创建文件*/
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
main()
{
int fd; char name[];
short age;
float score;
char sex; fd=open("test.dat",
O_RDWR|O_CREAT|O_EXCL,
);
if(fd==-) printf("open error:%m\n"),exit(-); //写第一条
memcpy(name,"tom",strlen("tom")+);
age=;
score=99.99;
sex='F'; write(fd,name,sizeof(name));
write(fd,&age,sizeof age);
write(fd,&score,sizeof(float));
write(fd,&sex,sizeof(sex)); //写第二条
memcpy(name,"Bush",strlen("Bush")+);
age=;
score=65.00;
sex='M';
write(fd,name,sizeof(name));
write(fd,&age,sizeof age);
write(fd,&score,sizeof(float));
write(fd,&sex,sizeof(sex)); //写第三条 memcpy(name,"达内",strlen("达内")+);
age=;
score=99.00;
sex='F';
write(fd,name,sizeof(name));
write(fd,&age,sizeof age);
write(fd,&score,sizeof(float));
write(fd,&sex,sizeof(sex)); close(fd);
}
案例3:
打开文件读取数据
重点:
怎么打开读取
文件尾的判定 基本类型的数据读写.
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h> main()
{
char name[];
short age;
float score;
char sex;
int fd;
int r;
fd=open("test.dat",O_RDONLY);
if(fd==-) printf("open error:%m\n"),exit(-); while()
{
r=read(fd,name,sizeof(name));
if(r==) break;
r=read(fd,&age,sizeof(short));
r=read(fd,&score,sizeof(float));
r=read(fd,&sex,sizeof(sex));
printf("%s,\t%4hd,\t%.2f,\t%1c\n",
name,age,score,sex);
} close(fd);
}
案例4:
结构体读取
描述:从键盘读取若干条数据,保存到文件
数据追加 #include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
struct stu
{
int no;
char name[];
float score;
};
/*
.判定文件是否存在,存在打开,不存在创建
.输入记录
.保存记录
.提示继续输入
.继续/不继续
.关闭文件
*/
int openfile(const char *filename)
{
int fd;
fd=open(filename,O_RDWR|O_CREAT|O_EXCL,);
if(fd==-)//表示文件存在,则打开
{
fd=open(filename,O_RDWR|O_APPEND);
return fd;
}
return fd;
}
void input(struct stu *record)
{
bzero(record,sizeof(struct stu));
printf("输入学号:");
scanf("%d",&(record->no));
printf("输入姓名:");
scanf("%s",record->name);
printf("输入成绩:");
scanf("%f",&(record->score));
}
void save(int fd,struct stu *record)
{
write(fd,record,sizeof(struct stu));
}
int iscontinue()
{
char c;
printf("是否继续输入:\n");
//fflush(stdin);
//fflush(stdout);
scanf("\n%c",&c);
if(c=='Y' || c=='y')
{
return ;
}
return ;
} int main()
{
int fd;
int r;
struct stu s={};
fd=openfile("stu.dat");
if(fd==-) printf("openfile:%m\n"),exit(-); while()
{
input(&s);
save(fd,&s);
r=iscontinue();
if(r==) break;
system("clear");
}
close(fd);
printf("输入完毕!\n");
}
.文件描述符号与重定向
.判定文件描述符号与终端的邦定关系
int isatty(int fd)
返回非0:fd输出终端
:fd输出被重定向
.防止重定向
/dev/tty #include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h> int main()
{
int fd;
printf("Hello\n");
write(,"World\n",);
fd=open("/dev/tty",O_WRONLY);
if(isatty())
{
write(,"notredir\n",);
}
else
{
write(,"redir\n",);
}
write(fd,"Killer\n",);
}
总结:
.make的多目标依赖规则以及伪目标
.文件的创建与打开(了解设置位的作用)
.文件的读写(字符串/基本类型/结构体)
.了解描述符号与重定向 作业:
.完成上课的练习.
.写一个程序使用结构体读取1种的数据,
并全部打印数据,
并打印平均成绩
.写一个程序:
查询1种的数据.比如:输入姓名,查询成绩
.写一个程序,录入保存如下数据:
书名 出版社 价格 存储量 作者
.写一个程序负责文件拷贝
main 存在的文件 新的文件名
要求:
文件存在就拷贝,不存在提示错误.
C 高级编程5 IO与文件权限的更多相关文章
- 《UNIX环境高级编程》笔记——4.文件和目录
一.引言 本章描述文件系统的其他特征和文件的性质.有些背景知识需要注意,例如用户ID与文件权限.文件系统等. 二.函数stat.fstat.fstatat和lstat #include <sys ...
- UNIX系统高级编程——第四章-文件和目录-总结
文件系统: 以UNIX系统V文件系统为例: 磁盘分为区,每个分区都有自己的文件系统: i节点是固定长度的记录项,包含了文件的相关信息.目录项包含文件名和i节点号.stat结构中除文件名和i节点编号 ...
- 《UNIX环境高级编程》笔记——3.文件IO
一.引言 说明几个I/O函数:open.read.write.lseek和close,这些函数都是不带缓冲(不带缓冲,只调用内核的一个系统调用),这些函数不输入ISO C,是POSIX的一部分: 多进 ...
- UNIX环境高级编程 第4章 文件和目录
第三章说明了关于文件I/O的基本函数,主要是针对普通regular类型文件.本章描述文件的属性,除了regular文件还有其他类型的文件. 函数stat.fstat.fstatat和lstat sta ...
- Node.js高级编程读书笔记 - 2 文件和进程处理
Outline 3 文件.进程.流和网络 3.1 查询和读写文件 3.2 创建和控制外部进程 3.3 读写数据流 3 文件.进程.流和网络 3.1 查询和读写文件 path 从Node 0.8起,pa ...
- UNIX环境高级编程 apue.h头文件的配置
http://jimslinbing.blog.163.com/blog/static/85054319201292712414518/ 1.到http://www.apuebook.com下载源码2 ...
- UNIX环境高级编程 第3章 文件I/O
前面两章说明了UNIX系统体系和标准及其实现,本章具体讨论UNIX系统I/O实现,包括打开文件.读文件.写文件等. UNIX系统中的大多数文件I/O只需要用到5个函数:open.read.write. ...
- UNIX环境高级编程 标准IO库
标准I/O库处理很多细节,使得便于用户使用. 流和 FILE 对象 对于标准I/O库,操作是围绕 流(stream)进行的.当用标准I/O打开或创建一个文件时,我们已使一个流与一个文件相关联. 对于A ...
- 《UNIX环境高级编程》读书笔记 —— 文件 I/O
打开或创建一个文件 #include <fcntl.h> int open(const char *pathname, int oflag, .../*mode_t mode*/); ...
随机推荐
- POJ 3683 Priest John's Busiest Day (2-SAT,常规)
题意: 一些人要在同一天进行婚礼,但是牧师只有1个,每一对夫妻都有一个时间范围[s , e]可供牧师选择,且起码要m分钟才主持完毕,但是要么就在 s 就开始,要么就主持到刚好 e 结束.因为人数太多了 ...
- apache开源项目--TIKA
Tika是一个内容抽取的工具集合(a toolkit for text extracting).它集成了POI, Pdfbox 并且为文本抽取工作提供了一个统一的界面.其次,Tika也提供了便利的扩展 ...
- 【转】android的消息处理机制(图+源码分析)——Looper,Handler,Message
原文地址:http://www.cnblogs.com/codingmyworld/archive/2011/09/12/2174255.html#!comments 作为一个大三的预备程序员,我学习 ...
- 【转】Application.mk 文件语法规范
原文网址:http://blog.sina.com.cn/s/blog_4c451e0e0100s6q4.html Application.mk file syntax specification A ...
- MEX文件编写和调试
作者kaien,2010/02/16 以前我写过一篇文章,详细的介绍过MEX的格式,语法,编译,调试等.可惜记不清放在哪里了.而最近又用到MEX编程,所以只能重新温习一番.时间有限,只记下简要流程和注 ...
- 设备扩展(DEVICE_EXTENSION)
原文链接:http://blog.csdn.net/hazy/article/details/481705 WDM中的结构 ---设备扩展 设备扩展(DEVICE_EXTENSION)是与设备对象 ...
- Tomcat 7 Connector 精读(2) CoyoteAdapter
这个适配器类只讲2个方法,构造方法中我们看到一个适配器对象有自己关联的连接器类. 其中Service的重要任务就是讲客户端端请求交给容器. public void service(org.apache ...
- 7.OpenACC
OpenACC: openacc 可以用于fortran, c 和 c++程序,可以运行在CPU或者GPU设备. openacc的代码就是在原有的C语言基础上进行修改,通过添加:compiler di ...
- [转载]C++命名规则
在软件开发这一高度抽象而且十分复杂的活动中,命名规则的重要性更显得尤为突出.一套定义良好并且完整的.在整个项目中统一使用的命名规范将大大提升源代码的可读性和软件的可维护性. 在引入细节之前,先说明一下 ...
- ubuntu源码安装R语言
下载后解压完,进入开始配置: ./configure --enable-R-shlib 报错: configure: error: con--with-readline=yes (default) a ...