man 手册
一、打开/创建文件 open
二、写入文件 write
三、关闭 close
四、创建文件 creat
五、读取文件 read
六、光标移动 lseek
七、计算文件大小占多少字节 filesize
文件描述符
文件编程的一般步骤
Linux文件管理简述
linux操作系统提供了一系列的API
自动化 函数
实际工作中都要注意去判断返回值以免出错
man 手册
SYNOPSIS 概要
DESCRIPTION 描述
RETURN VALUE 返回值
ERRORS 错误
open 打开
write/read 读写
lseek 光标定位
close 关闭
pathname 路径
flags 标志
mode (模式)权限
fd/Filedescriptor 文件描述符
buf 缓冲区
size_t count 读写大小
offset 偏移量
whence 偏移位置
一、打开/创建文件 open
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 可读可写打开
当我们附带了权限后,打开的文件就只能按照这种权限来操作。
以上这三个常数中应当只指定一 个。下列常数是可选择的:
O_CREAT 若文件不存在则创建它。使用此选项时,需要同时说明第三个参数mode,用其说明该新文件的存取许可权限。
O_EXCL 如果同时指定了O_CREAT,而文件已经存在,则出错。
O_APPEND 每次写时都加到文件的尾端。
O_TRUNC 属性去打开文件时,如果这个文件中本来是有内容的,而且为只读或只写成功打开,则将其长度截短为0。
Mode:一定是在flags中使用了O_CREAT标志,mode记录待创建的文件的访问权限
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main()
{
int fd;
//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);
fd = open("./file1",O_RDWR);
printf("fd = %d\n",fd);
return 0;
}
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main()
{
int fd;
fd = open("./file1",O_RDWR|O_CREAT|O_EXCL,0600);
if(fd == -1){
printf("file exist\n");
}
return 0;
}
0600表示可读可写
#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.\n");
}
}
return 0;
}
二、写入文件 write
SYNOPSIS
#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);
三、关闭 close
SYNOPSIS
#include <unistd.h>
int close(int fd);
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.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");
}
}
printf("open success : fd = %d\n",fd);
//ssize_t write(int fd, const void *buf, size_t count);
write(fd,buf,strlen(buf));
//int close(int fd);
close(fd);
return 0;
}
O_RDWR文件如果有内容就会被覆盖掉。
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int fd;
char *buf = "hello world !";
fd = open("./file1",O_RDWR);
printf("open success : fd = %d\n",fd);
//ssize_t write(int fd, const void *buf, size_t count);
int n_write = write(fd,buf,strlen(buf));
if(n_write != -1){
printf("write %d byte to file\n",n_write);
}
//int close(int fd);
close(fd);
return 0;
}
O_APPEND 每次写时都加到文件的尾端。
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int fd;
char *buf = "hello world !";
fd = open("./file1",O_RDWR|O_APPEND);
printf("open success : fd = %d\n",fd);
//ssize_t write(int fd, const void *buf, size_t count);
int n_write = write(fd,buf,strlen(buf));
if(n_write != -1){
printf("write %d byte to file\n",n_write);
}
//int close(int fd);
close(fd);
return 0;
}
O_TRUNC 属性去打开文件时,如果这个文件中本来是有内容的,而且为只读或只写成功打开,则将其长度截短为0。
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int fd;
char *buf = "test";
fd = open("./file1",O_RDWR|O_TRUNC);
printf("open success : fd = %d\n",fd);
//ssize_t write(int fd, const void *buf, size_t count);
int n_write = write(fd,buf,strlen(buf));
if(n_write != -1){
printf("write %d byte to file\n",n_write);
}
//int close(int fd);
close(fd);
return 0;
}
四、创建文件 creat
SYNOPSIS
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int creat(const char *pathname, mode_t mode);
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int fd;
char *buf = "test";
//int creat(const char *pathname, mode_t mode);
fd = creat("./file2",S_IRWXU);
return 0;
}
五、读取文件 read
SYNOPSIS
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);
SYNOPSIS
#include <stdlib.h>
void *malloc(size_t size);
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.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");
}
}
printf("open success : fd = %d\n",fd);
//ssize_t write(int fd, const void *buf, size_t count);
int n_write = write(fd,buf,strlen(buf));
if(n_write != -1){
printf("write %d byte to file\n",n_write);
}
//关闭重新打开光标移动到头
close(fd);
fd = open("./file1",O_RDWR);
char *readBuf;
readBuf = (char *)malloc(sizeof(char)*n_write + 1);
//ssize_t read(int fd, void *buf, size_t count);
int n_read = read(fd,readBuf,n_write);
printf("read %d,context:%s\n",n_read,readBuf);
//int close(int fd);
close(fd);
return 0;
}
write写入完文件内容光标移动到了末尾,直接使用read读取文件内容读取不到
六、光标移动 lseek
除了上述关闭文件,再打开文件,让光标回到文件头Linux提供了一个lseek的API
SYNOPSIS
#include <sys/types.h>
#include <unistd.h>
off_t lseek(int fd, off_t offset, int whence);
DESCRIPTION
lseek() repositions the file offset of the open file description asso‐
ciated with the file descriptor fd to the argument offset according to
the directive whence as follows:
SEEK_SET
The file offset is set to offset bytes.
SEEK_CUR
The file offset is set to its current location plus offset
bytes.
SEEK_END
The file offset is set to the size of the file plus offset
bytes.
offset 偏移量
chence 偏移位置
将文件读写指针相对whence移动offset个字节
SEEK_SET 开头
SEEK_CUR 当前光标位置
SEEK_END 末尾
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.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");
}
}
printf("open success : fd = %d\n",fd);
//ssize_t write(int fd, const void *buf, size_t count);
int n_write = write(fd,buf,strlen(buf));
if(n_write != -1){
printf("write %d byte to file\n",n_write);
}
char *readBuf;
readBuf = (char *)malloc(sizeof(char)*n_write + 1);
//off_t lseek(int fd, off_t offset, int whence);
lseek(fd,0,SEEK_SET);
//ssize_t read(int fd, void *buf, size_t count);
int n_read = read(fd,readBuf,n_write);
printf("read %d,context:%s\n",n_read,readBuf);
//int close(int fd);
close(fd);
return 0;
}
七、计算文件大小占多少字节 filesize
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int fd;
char *buf = "hello world !";
fd = open("./file1",O_RDWR);
int filesize = lseek(fd, 0, SEEK_END);
printf("file's size is:%d\n",filesize);
close(fd);
return 0;
}
文件描述符
Linux系统默认:0标准输入、1标准输出、2标准错误
#defind STDIN_FILENO 0
#defind STDOUT_FILENO 1
#defind STDERR_FILENO 2
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int fd;
char readBuf[128];
//ssize_t read(int fd, void *buf, size_t count);
int n_read = read(0,readBuf,5);
//ssize_t write(int fd, const void *buf, size_t count);
int n_write = write(1,readBuf,strlen(readBuf));
printf("\ndone!\n");
return 0;
}
文件编程的一般步骤
- 打开/创建文件
- 读取文件/写入文件
- 关闭文件
1.静态文件存在磁盘中
2. 动态文件(open静态文件以后)
3.会在Linux内核在进程中建立一个打开文件数据结构(结构体)来记录这个文件
- 文件的fd
- 信息节点
- buf对应内存(内容,缓存)
- 所有后面的read/write都是对buf内存的操作(都是对动态文件进行操作)
没有对磁盘进行操作4.只有在close之后才会把buf缓存区的内容写到磁盘里去(保存到磁盘)
Linux文件管理简述
用户空间 User Space
-应用 Application
内核空间 Kernel Space
-系统调用 SCI(System Call Interace)
-虚拟文件系统 VFS(Virtual File System)
-内线 EXT
-BTRFS(Butter FS) COW(copy-on-write式)文件系统
-常规块设备层 General Block Device Layer
-驱动程序 Device Driver
硬件 Hard Ware
-物理磁盘 Physical Disk