三.基于文件的描术符号
.得到文件描术符号/释入文件描术符号
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与文件权限的更多相关文章

  1. 《UNIX环境高级编程》笔记——4.文件和目录

    一.引言 本章描述文件系统的其他特征和文件的性质.有些背景知识需要注意,例如用户ID与文件权限.文件系统等. 二.函数stat.fstat.fstatat和lstat #include <sys ...

  2. UNIX系统高级编程——第四章-文件和目录-总结

    文件系统: 以UNIX系统V文件系统为例: 磁盘分为区,每个分区都有自己的文件系统: ​ i节点是固定长度的记录项,包含了文件的相关信息.目录项包含文件名和i节点号.stat结构中除文件名和i节点编号 ...

  3. 《UNIX环境高级编程》笔记——3.文件IO

    一.引言 说明几个I/O函数:open.read.write.lseek和close,这些函数都是不带缓冲(不带缓冲,只调用内核的一个系统调用),这些函数不输入ISO C,是POSIX的一部分: 多进 ...

  4. UNIX环境高级编程 第4章 文件和目录

    第三章说明了关于文件I/O的基本函数,主要是针对普通regular类型文件.本章描述文件的属性,除了regular文件还有其他类型的文件. 函数stat.fstat.fstatat和lstat sta ...

  5. Node.js高级编程读书笔记 - 2 文件和进程处理

    Outline 3 文件.进程.流和网络 3.1 查询和读写文件 3.2 创建和控制外部进程 3.3 读写数据流 3 文件.进程.流和网络 3.1 查询和读写文件 path 从Node 0.8起,pa ...

  6. UNIX环境高级编程 apue.h头文件的配置

    http://jimslinbing.blog.163.com/blog/static/85054319201292712414518/ 1.到http://www.apuebook.com下载源码2 ...

  7. UNIX环境高级编程 第3章 文件I/O

    前面两章说明了UNIX系统体系和标准及其实现,本章具体讨论UNIX系统I/O实现,包括打开文件.读文件.写文件等. UNIX系统中的大多数文件I/O只需要用到5个函数:open.read.write. ...

  8. UNIX环境高级编程 标准IO库

    标准I/O库处理很多细节,使得便于用户使用. 流和 FILE 对象 对于标准I/O库,操作是围绕 流(stream)进行的.当用标准I/O打开或创建一个文件时,我们已使一个流与一个文件相关联. 对于A ...

  9. 《UNIX环境高级编程》读书笔记 —— 文件 I/O

    打开或创建一个文件 #include <fcntl.h> int open(const char *pathname, int oflag, .../*mode_t mode*/);    ...

随机推荐

  1. 剑指Offer:找出数组中出现次数超过一半的元素

    题目:找出数组中出现次数超过一半的元素 解法:每次删除数组中两个不同的元素,删除后,要查找的那个元素的个数仍然超过删除后的元素总数的一半 #include <stdio.h> int ha ...

  2. Codeforces 629C Famil Door and Brackets DP

    题意:给你一个由括号组成的字符串,长度为m,现在希望获得一个长度为n(全由括号组成)的字符串,0<=n-m<=2000 这个长度为n的字符串要求有两个性质:1:就是任意前缀,左括号数量大于 ...

  3. Tomcat 7 Connector 精读(1)

    这个类图是本人截取的最重要的类的方法和属性. 其中ProtocalHandler是协议处理器,tomcat支持的协议以下方法可以看到.不同协议实现了不同的ProtocalHandler类. publi ...

  4. python bisect模块

    转发:http://www.cnblogs.com/skydesign/archive/2011/09/02/2163592.html 先看看模块的结构: 前面五个属性大家感兴趣可以打出来看看数值,这 ...

  5. 对Map按key和value分别排序

    一.理论准备         Map是键值对的集合接口,它的实现类主要包括:HashMap,TreeMap,Hashtable以及LinkedHashMap等.         TreeMap:基于红 ...

  6. North America Qualifier (2015)

    https://icpc.baylor.edu/regionals/finder/north-america-qualifier-2015 一个人打.... B 概率问题公式见代码 #include ...

  7. elecworks中“插入点”的意思

    elecworks中自建符号时,”插入点“的用法: 插入点的作用是:建好的符号加入符号库后,从库里调用符号到原理图中时“插入点”就是符号的中心点---鼠标光标拖动的点. (插入点即为符号调出 时插入图 ...

  8. [Objective-c 基础 - 2.5] .h和.m文件,点语法,成员变量作用域

    A. 命令行编译和XCode编译 1.在存在多个.m源码实现文件的情况下,若根据需要引入.m文件,使用命令行仅仅编译主.m文件即可,但是XCode会同时编译全部.m文件,会发生编译错误(重复定义全局变 ...

  9. RC522天线匹配参数【worldsing笔记】

    图为Device读卡器的参数值 EMC电路对读写距离影响不大:                   L3 和L4 固定为2.2uH:                  C11和C12也是固定值,如果P ...

  10. iOS几个效果动画-------------------(实例详讲)qq粘性效果

    这几天做了一些简单iOS的效果图,感觉苹果官方已经帮我们做了很多了,我们只是站在巨人的肩膀上编程,这些也没什么难的,最难的也就是用到了初中的三角函数,先让大家看看这几个动画吧.先列这几个把,由上而下分 ...