Linux下的文件描述符

什么是文件描述符

当我们在Linux下打开一个文件时,这个文件的内容就会被读取到内存中来,而Linux下有一个列表来管理这些内存,这个列表里面存储的就是这些空间对应的指针,所以说文件描述符的本质就是存这个文件内容的内存指针对应在链表的下标(索引),最基本的三个文件描述符就是标准输入(stdin,文件描述符0,一般就是键盘),标准输出(stdout,文件描述符1,一般输出到屏幕),标准错误输出(stderr,文件描述符2,一般也是屏幕),我们调用print()一般就是调用文件描述符1。

如何使用文件描述符

1. 打开文件(获取文件描述符)

使用open()系统调用打开文件,并获取文件描述符。

#include <fcntl.h>

#include <unistd.h>

int fd = open("example.txt", O_RDONLY);

if (fd == -1) {

perror("open");

return 1;

}

  1. 读取文件

使用read()系统调用来从文件描述符指向的文件中读取数据。

char buffer[1024];

ssize_t bytesRead = read(fd, buffer, sizeof(buffer));

if (bytesRead == -1) {

perror("read");

return 1;

}

3. 写入文件

使用write()系统调用来向文件描述符指向的文件中写入数据。

复制代码
const char *text = "Hello, World!\n";
ssize_t bytesWritten = write(fd, text, sizeof(text));
if (bytesWritten == -1) {
    perror("write");
    return 1;
}

4. 移动文件指针

使用lseek()系统调用来移动文件描述符指向的文件的文件指针。

复制代码
off_t offset = lseek(fd, 0, SEEK_SET);
if (offset == (off_t)-1) {
    perror("lseek");
    return 1;
}

5. 关闭文件

使用close()系统调用来关闭文件描述符,释放资源。

复制代码
if (close(fd) == -1) {
    perror("close");
    return 1;
}

8. 管道和套接字

文件描述符也用于管道和套接字,使用pipe()socket()系统调用来创建管道和套接字。

复制代码
int pipe_fds[2];
if (pipe(pipe_fds) == -1) {
    perror("pipe");
    return 1;
}

int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
    perror("socket");
    return 1;
}

文件描述符的重定向

我们可以对已经存在的文件描述符进行重定向,比如文件描述符1,默认是将向屏幕输送信息,可以将他更改为向某个文件输送,这样,调用printf()函数就会向对应文件输入信息,重定义的系统调用一般使用dup2()

#include <unistd.h>

#include <fcntl.h>

int main() {

int new_stdout_fd = open("output.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);

if (new_stdout_fd == -1) {

perror("open");

return 1;

}

if (dup2(new_stdout_fd, STDOUT_FILENO) == -1) {

perror("dup2");

return 1;

}

// 现在,stdout被重定向到output.txt

printf("This will be written to output.txt\n");

// 关闭新文件描述符,因为dup2已经复制了它

close(new_stdout_fd);

return 0;

}

相关推荐
saltymilk17 小时前
C++ 模板参数推导问题小记(模板类的模板构造函数)
c++·模板元编程
感哥17 小时前
C++ lambda 匿名函数
c++
沐怡旸1 天前
【底层机制】std::unique_ptr 解决的痛点?是什么?如何实现?怎么正确使用?
c++·面试
感哥1 天前
C++ 内存管理
c++
CYRUS_STUDIO1 天前
用 Frida 控制 Android 线程:kill 命令、挂起与恢复全解析
android·linux·逆向
熊猫李1 天前
rootfs-根文件系统详解
linux
博笙困了1 天前
AcWing学习——双指针算法
c++·算法
感哥1 天前
C++ 指针和引用
c++
dessler1 天前
Hadoop HDFS-高可用集群部署
linux·运维·hdfs
泽泽爱旅行1 天前
awk 语法解析-前端学习
linux·前端