三.基于文件的描术符号
.得到文件描术符号/释入文件描术符号
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. POJ 3281 Dining (网络流构图)

    [题意]有F种食物和D种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料.现在有N头牛,每头牛都有自己喜欢的食物种类列表和饮料种类列表,问最多能使几头牛同时享用到自己喜欢的食物和 ...

  2. 【转】提示框第三方库之MBProgressHUD iOS toast效果 动态提示框效果

    原文网址:http://www.zhimengzhe.com/IOSkaifa/37910.html MBProgressHUD是一个开源项目,实现了很多种样式的提示框,使用上简单.方便,并且可以对显 ...

  3. undo日志

    InnoDB’s Undo 前言 Undo log是InnoDB MVCC事务特性的重要组成部分.当我们对记录做了变更操作时就会产生undo记录,Undo记录默认被记录到系统表空间(ibdata)中, ...

  4. VirtualBox Headless启动虚拟机

    习惯了在Windows上来学习和娱乐,所以不能切换到Linux系统. 为了Linux编程,我首先尝试了wubi在Windows上安装双系统,但是发现本来启动很快的Windows8安装了双系统之后,系统 ...

  5. LIBSVM的使用方法

    [原文:http://wenku.baidu.com/view/7e7b6b896529647d27285276.html] 目  录 1 Libsvm下载... 3 2 Libsvm3.0环境变量设 ...

  6. NOIP2009 最优贸易

    3. 最优贸易 (trade.pas/c/cpp) [问题描述] C 国有 n 个大城市和 m 条道路,每条道路连接这 n 个城市中的某两个城市.任意两个城市之间 多只有一条道路直接相连.这 m 条道 ...

  7. JavaScript中值的真真假假(true and false)

    值为flase的有: false 0 "" //空串 null undefined NaN 除了以上的之外的都是ture,包括"0" (zero in quot ...

  8. JM编解码264

    看到有人说JM解码编码264 尝试了一下http://iphome.hhi.de/suehring/tml/download/win7下 vs2010 编译后,得到编码解码可执行文件ldecod.ex ...

  9. nyoj 811 变态最大值

    变态最大值 时间限制:1000 ms  |  内存限制:65535 KB 难度:1   描述 Yougth讲课的时候考察了一下求三个数最大值这个问题,没想到大家掌握的这么烂,幸好在他的帮助下大家算是解 ...

  10. stm32 cortext-M3 类型对齐问题【worldsing笔记】

    经过细测,Cortex-M3的double类型必须4字节对齐访问,其他诸如float,int,short 可以非对齐访问.否则将会产生硬件异常!即访问double类型地址必须能被4整除,测试代码如下: ...