3.5 fdopen()和fileno()函数 http://book.2cto.com/201212/11763.html

本文介绍了如何使用fdopen()函数将文件描述字与流连接起来,并通过fileno()函数获取流对应的文件描述字。fdopen()允许为已打开的文件描述字创建标准I/O缓冲,而fileno()则用于确定流的底层文件描述字。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文件描述字函数是流函数的初等函数,每一个流都与一个描述字相连。给定一个打开的文件描述字,可以用fdopen()函数为它创建一个流。反过来,已知一个流,也可以用fileno()函数得到它的文件描述字。
#include <stdio.h>
FILE *fdopen (int filedes, const char * opentype);
int fileno (file * stream);

fdopen()使描述字filedes与一个流相连。它的返回值是一个新的流,如果不能创建此流,则返回空指针。

参数opentype的取值与fopen()的opentype参数完全相同,但“w”和“w+”不导致文件截断,因为截断是文件打开时的动作,而在此情形下,文件已经被打开。注意,调用fdopen()时必须保证opentype参数与打开文件描述字时使用的opentype参数一致。

fdopen()建立的新流的文件位置与描述字filedes的文件位置相同,且流的错误指示器和文件结束指示器均被清除。fdopen()的实质是为已打开的文件描述字提供标准I/O缓冲。

fileno()函数返回与流stream相连的文件描述字。利用它可以确定流的底层文件描述字。例如,当调用dup()或fcntl()时就需要知道与流相连的文件描述字。

In file included from /workspace/x2/dfx/modules/hiviewx_watcher/component/log_watcher/main.cpp:6: /workspace/x2/dfx/modules/hiviewx_watcher/component/log_watcher/include/glog_sink.h: In member function ‘void CustomFileSink::ReopenLogFile(): /workspace/x2/dfx/modules/hiviewx_watcher/component/log_watcher/include/glog_sink.h:250:43: error: cannot convert ‘std::basic_ofstream<char>::__filebuf_type*’ {aka ‘std::basic_filebuf<char>*’} to ‘FILE*’ 250 | int fd = fileno(outfile_.rdbuf()); | ~~~~~~~~~~~~~~^~ | | | std::basic_ofstream<char>::__filebuf_type* {aka std::basic_filebuf<char>*} In file included from /usr/include/c++/11/cstdio:42, from /usr/include/c++/11/ext/string_conversions.h:43, from /usr/include/c++/11/bits/basic_string.h:6608, from /usr/include/c++/11/string:55, from /usr/include/c++/11/stdexcept:39, from /usr/include/boost/asio/execution/receiver_invocation_error.hpp:19, from /usr/include/boost/asio/execution/detail/as_invocable.hpp:22, from /usr/include/boost/asio/execution/execute.hpp:20, from /usr/include/boost/asio/execution/executor.hpp:20, from /usr/include/boost/asio/associated_executor.hpp:20, from /usr/include/boost/asio.hpp:21, from /workspace/x2/dfx/modules/hiviewx_watcher/component/log_watcher/main.cpp:2: /usr/include/stdio.h:809:26: note: initializing argument 1 of ‘int fileno(FILE*)’ 809 | extern int fileno (FILE *__stream) __THROW __wur; | ~~~~~~^~~~~~~~ make[2]: *** [modules/hiviewx_watcher/component/log_watcher/CMakeFiles/log_watcher.dir/build.make:76: modules/hiviewx_watcher/component/log_watcher/CMakeFiles/log_watcher.dir/main.cpp.o] Error 1 make[1]: *** [CMakeFiles/Makefile2:585: modules/hiviewx_watcher/component/log_watcher/CMakeFiles/log_watcher.dir/all] Error 2 make: *** [Makefile:136: all] Error 2 ================================================== 这是我的报错信息. void ReopenLogFile() { std::lock_guard<std::mutex> lock(file_mutex_); // 先关闭现有文件 if (outfile_.is_open()) { outfile_.flush(); outfile_.close(); } // 确保目录存在 auto dir_path = std::filesystem::path(log_file_path_).parent_path(); if (!dir_path.empty()) { std::filesystem::create_directories(dir_path); } // 重新打开文件 outfile_.open(log_file_path_, std::ios::out | std::ios::app); if (!outfile_.is_open()) { std::cerr << "[ERROR] Failed to open log file: " << log_file_path_ << " - " << strerror(errno) << std::endl; } else { std::cout << "[DEBUG] Successfully reopened log file: " << log_file_path_ << std::endl; // 获取底层文件描述符 (仅用于调试) int fd = fileno(outfile_.rdbuf()); std::cout << "[DEBUG] New file descriptor: " << fd << std::endl; // 强制立即写入到磁盘 fsync(fd); } }这是我的代码.错误如何修改呢
最新发布
07-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值