8种机械键盘轴体对比
本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选?
本文介绍了文件IO的操作!
代码例程: 例程
参考:《UNIX环境高级编程》
介绍
根据UNIX的体系结构,从内到外依次是内核、系统调用、shell、库函数、和应用程序。内核的接口成为系统调用。程序运行内存空间可以分为用户空间和内核空间。应用程序工作在用户空间、如果应用程序想要访问内核空间的话,可以使用系统调用安全的访问内核空间。
Linux提供了一系列系统调用函数,常用的有open、write、read、lseek、和close。本文介绍的文件IO也称为不带缓冲的IO。
文件描述符
在Linux中一切皆文件,所以打开的文件都会返回一个文件描述符引用。文件描述符是一个非负整数。在POSIX应用程序中,0 1 2被替换成了符号常数0STDERR_FILENO(标准错误输出1TDIN_FILENO(标准输入)
2STDERR_FILENO(标准错误输出)
文件IO基本操作
open函数1
2
3
4头文件
#include
int (const char *path, int oflags, mode_t mode);open函数用来打开或创建文件。
path 是打开文件的名字,oflags 打开文件所要采取的动作,mode 文件访问的格式
1
2
3
4
5
6
7
8oflags:(可位与)
O_RDONLY 只读方式打开文件
O_WRONLY 只写方式打开文件
O_RDWR 以读写方式打开文件
O_APPEND 把写入数据追加到文件末尾
O_TRUNC 把文件长度设置为0,丢弃已有内容
O_CREAT 如需要就按参数mode给出的访问模式创建文件
O_EXCL 与O_CREAT一起使用,确保调用者创建出文件。open调用是一个原子操作,它只执行一个函 数调用。使用这个可选模式可以防止两个程序同时创建一同一个文件。如果文件已经存在,open调用失败
mode 是访问权限
返回值的文件描述符取值
0打开或创建成功
-1失败
write函数1
2#include
size_t write(int fd, const void *buf, size_t nbytes);打开文件成功之后,可以往文件里写数据
fd 文件描述符,buf写入数据在内存空间的起始地址,写入字节数
返回值的文件描述符取值
非负整数写入成功,返回写入字节数
-1失败
read函数1
2#include
size_t read(int fd, void *buf, size_t nbytes);打开文件成功之后,可以从文件里读数据
fd 文件描述符,buf读数据在内存空间的起始地址,读出最大字节数
返回值的文件描述符取值
非负整数读成功,返回读出字节数
-1失败
lseek函数1
2
3#include
#include
off_t lseek(int fd, off_t offset, int whence);每次打开或新建一个文件,偏移量就从文件开始处,在同一次的打开操作之后,读写就会对其偏移量增加
fd 是文件描述符, offset 是偏移量,whence是模式
1
2
3
4whence 取值:
SEEK_SET 绝对位置
SEEK_CUR当前位置
SEEK_END 文件尾
stat函数1
2
3#include
int stat(const char *file_name, struct stat *buf);这个函数是通过文件名,得到存储文件信息的结构体buf
成功返回0 ,失败返回-1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15struct stat {
dev_t st_dev;
ino_t st_ino; //节点
mode_t st_mode; //文件的类型和存取的权限
nlink_t st_nlink; //连到该文件的硬连接数目,刚建立的文件值为1
uid_t st_uid; //用户ID
gid_t st_gid; //组ID
dev_t st_rdev; //(设备类型)若此文件为设备文件,则为其设备编号
off_t st_size; //文件字节数(文件大小)
unsigned long st_blksize; //块大小(文件系统的I/O 缓冲区大小)
unsigned long st_blocks; //块数
time_t st_atime; //最后一次访问时间
time_t st_mtime; //最后一次修改时间
time_t st_ctime; //最后一次改变时间(指属性)
};
close函数1
2#include
int close(int fd);操作完一个函数之后应该,显式的关闭一打开文件
例子新建文本1,写入一段话1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41Traveling Light
I was doubling over the load on my shoulders
was a weight I carried with me everyday
Crossing miles of frustrations and rivers of raging
Picking up stones I found along the way
I staggered and I stumbled down
pathway of trouble I was
hauling those souvernirs of misery
And with each step taken my back was breaking
Till I found the One who took it all from me
Down by the riverside(Down by the riverside)
I laid my burdens down,Now I'm traveling light
My spirit lifted high(I found my freedom now)
I found my freedom now,And I'm traveling light
Through the darkest alleys and loneliest valleys
I was dragging those heavy chains of doubt and fear
Then with the one word spoken the locks were broken
Now He's leading me to places
Where there are no tears
Down by the riverside
(Down by the riverside)
I laid my burdens down,
Now I'm traveling light
My spirit lifted high
(I found my freedom now)
I found my freedom now
And I'm traveling light
Down by the riverside
I laid my burdens down
Now I'm traveling light
My spirit lifted high
I found my freedom now
And I'm traveling light
Down by the riverside
(Down by the riverside)
I laid my burdens down,
Now I'm traveling light
My spirit lifted high
(I found my freedom now)
I found my freedom now
And I'm traveling light
新建文本21
2
3歌曲名:Traveling Light
歌手名:Joel Hanson
专辑名:What If It Is
将文本1和文本2合并,并且生成文本3
代码:
combine.c 使用了open 、write、 read、lseek、close、和stat 函数,对文件操作