❀1. open函数
- 包含头文件
#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);
-
函数功能
打开一个文件,并返回文件描述符。
-
函数参数
-
pathname:文件名
-
flags:
-
必选参数(下面三个必须要有一个)
- O_RDONLY :只读
- O_WRONLY :只写
- O_RDWR :可读可写
-
可选参数(仅列出常用参数)
- O_APPEND :追加的方式打开,The file is opened in append mode.
- O_CREAT :如果文件不存在则创建,If the file does not exist it will be created.
- O_EXCL :和O_CREAT一块使用,如果文件存在则报错,if this flag is specified in conjunction with O_CREAT, and pathname already exists, then open() will fail.
- O_NONBLOCK :非阻塞的方式打开文件
-
-
mode:权限位 (实际的权限是mode & ~umask的结果)
-
-
函数返回值
返回最小的空闲的文件描述符,如果失败则返回-1并设置errno,fopen() and creat() return the new file descriptor, or -1 if an error occurred (in which case, errno is set appropriately).
❀2. close函数
- 包含头文件
#include <unistd.h>
- 函数原型
int close(int fd);
-
函数功能
close() closes a file descriptor, so that it no longer refers to any file and may be reused.
-
函数参数
fd :一个文件的文件描述符
-
函数返回值
成功返回0,失败返回-1且设置errno,close() returns zero on success. On error, -1 is returned, and errno is set appropriately.
❀3. 使用open与close实现touch命令
/************************************************************
>File Name : mtouch.c
>Author : QQ
>Company : QQ
>Create Time: 2022年05月12日 星期四 19时48分14秒
************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char* argv[])
{
if(argc < 2)
{
printf("not found file name\n");
return -1;
}
int i = 0;
for(i = 1; i < argc; i++)
{
int fd = open(argv[i], O_RDONLY | O_CREAT, 0666);
close(fd);
}
return 0;
}
实际上main函数也是有参数和返回值的,只不过我们在平时的学习中可能很少用到,main的返回值是int类型的,main函数的参数在Linux下编程用的还是比较多的。我们在运行一个可执行文件的时候可以在命令行传入参数给argv[],也就是说argv[]是用来存放我们在命令行传入的参数的,而参数argc用于统计参数的个数。不管我们传不传参数, argv[0]默认就是程序运行的路径名。也就是说argc最小为1(命令行不传参),argv[0]是程序运行路径。
❀4. read函数
- 包含头文件
#include <unistd.h>
- 函数原型
ssize_t read(int fd, void *buf, size_t count);
-
函数功能
read() attempts to read up to count bytes from file descriptor fd into the buffer starting at buf.
-
函数参数
- fd :文件描述符
- buf:缓冲区
- count:缓冲区大小
-
函数返回值
- 读取失败返回-1,同时设置errno 。如果非阻塞的情况下返回-1,需要判断errno的值
- 成功则返回读到的字节数(0表示已经读到文件末尾)
❀5. write函数
- 包含头文件
#include <unistd.h>
- 函数原型
ssize_t write(int fd, const void *buf, size_t count);
-
函数功能
write() writes up to count bytes from the buffer pointed buf to the file referred to by the file descriptor fd.
-
函数参数
- fd :文件描述符
- buf:缓冲区
- count:写入的字节数
-
函数返回值
- 写入失败返回-1,同时设置errno
- 写入成功则返回写入的字节数(0表示未写入)
❀6. 使用read和write实现cat命令
/************************************************************
>File Name : mcat.c
>Author : QQ
>Company : QQ
>Create Time: 2022年05月13日 星期五 12时11分44秒
************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define BUF_MAX 512 /*buf缓冲区最大值*/
#define FILE_MAX 5 /*可以查看的最大文件数*/
int main(int argc, char* argv[])
{
if(argc < 2)
{
printf("not fount file name");
return -1;
}
if(argc - 1 > FILE_MAX)
{
printf("too many filenames\n");
return -1;
}
int i = 0;
int fd[FILE_MAX];
char buf[BUF_MAX];
int read_size = 0;
memset(buf, 0, BUF_MAX);
for(i = 0; i < (argc - 1); i++)
{
fd[i] = open(argv[1 + i], O_RDONLY);
read_size = read(fd[i], buf, sizeof(buf));
write(STDOUT_FILENO, buf, read_size); /*STDOUT_FILENO是标准输出文件描述符1的宏定义*/
}
for(i = 0; i < (argc - 1); i++)
{
close(fd[i]);
}
return 0;
}
功能测试