1.文件编程概述
文件系统原理及访问机制
文件在内核中的管理机制
什么事文件信息节点inode
文件的共享
文件权限,其他用户对其权限
(应用为王)
Linux一切皆文件
文件系统(文件夹/文件)硬件设备 管道 数据库 Socket等
操作系统提供了一系列的API
如Linux系统:
- 打开 open
- 读写 write/read
- 光标定位 lseek
- 关闭 close
2.文件打开及创建
原型
NAME
open, creat - open and possibly create a file or device
SYNOPSIS
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
int creat(const char *pathname, mode_t mode);
参数说明:
Pathname:要打开的文件名(含路径,缺省为当前路径)
Flags:
- O_RDONLY 只读打开
- O_WRONLY 只写打开
- O_RDWR 可读可写可打开
代码1:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main()
{
int fd;
fd = open("./file1", 0600);
if(fd == -1)
{
printf("open file1 failed\n");
}
return 0;
}
结果:
open file1 failed
代码2:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main()
{
int fd;
fd = open("./file1", O_RDWR);
if(fd == -1)
{
printf("open file1 failed\n");
fd = open("./file1",O_RDWR|O_CREAT, 0600);//此处的权限
if(fd > 0)
{
printf("create file1 success!");
}
}
return 0;
}
结果:
open file1 failed
create file1 success!
并且多了一个file1的文件
a.out demo1.c demo2.c file1 lianbiao.c
- 可读 r 4
- 可写 w 2
- 执行 x 1
0600
当前用户 用户组 其他用户
4+2
可读可写
通过
ls -l
可以查看文件的权限“-”表示普通文件
-rwxrwxr-x 1 book book 8427 Sep 28 17:06 a.out
-rw-rw-r-- 1 book book 226 Sep 28 16:39 demo1.c
-rw-rw-r-- 1 book book 369 Sep 28 17:06 demo2.c
-rw------- 1 book book 0 Sep 28 17:06 file1
-rw-rw-r-- 1 book book 1177 Sep 28 12:27 lianbiao.c
3.文件写入操作编程
原型
NAME
write - write to a file descriptor
SYNOPSIS
#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);
记得关闭文件
NAME
close - close a file descriptor
SYNOPSIS
#include <unistd.h>
int close(int fd);
代码:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main() {
int fd;
char *buf = "hello world!";
fd = open("./file1", O_RDWR);
if (fd == -1) {
printf("open file1 failed\n");
fd = open("./file1", O_RDWR | O_CREAT, 0600);
if (fd > 0) {
printf("create file1 success!\n");
}
}
write(fd, buf, strlen(buf));//此处不能用sizeof因为指针是根据操作系统占字节大小
printf("open filed success!\n");
close(fd);
return 0;
}
宏表示 | 数字 | |
---|---|---|
S_IRUSR | 4 | 可读 |
S_IWUSR | 2 | 可写 |
S_IXUSR | 1 | 可执行 |
S_IRWXU | 7 | 可读、写、执行 |
4.文件读取操作
原型
NAME
read - read from a file descriptor
SYNOPSIS
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);
代码:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
int main() {
int fd;
char *buf = "hello world!";
fd = open("./file1", O_RDWR);
if (fd == -1) {
printf("open file1 failed\n");
fd = open("./file1", O_RDWR | O_CREAT, 0600);
if (fd > 0) {
printf("create file1 success!\n");
}
}
int n_write = write(fd, buf, strlen(buf));
if(n_write != -1)
{
printf("write %d bytes to buf, context:%s\n", n_write, buf);
}
close(fd);//注意关闭文件,不然将读不到信息,光标问题
open("./file1", O_RDWR);
char *readBuf = (char *)malloc(sizeof(char)*n_write + 1);
int n_read = read(fd, readBuf, n_write);
printf("read from readBuf %d bytes, context:%s\n", n_read, readBuf);
printf("open filed success!\n");
close(fd);
return 0;
}
结果:
open file1 failed
create file1 success!
write 12 bytes to buf, context:hello world!
read from readBuf