当前位置:网站首页>三种文件句柄之间的转换
三种文件句柄之间的转换
2022-06-21 23:52:00 【winux】
<- _fdopen <- <- _open_osfhandle
FILE* int fid OS HANDLE
-> _fileno -> -> _get_osfhandle
第一种FILE*是C语言里标准的文件指针。我们常说的stdin, stdout, stderr就是FILE*类型的
第二种是POSIX接口中的文件句柄。MSVC里提供的函数_open就返回这个句柄。(顺便提一句,Windows也是号称遵循POSIX标准的,虽然改的乱七八糟,毕竟还是有)。 其实stdin对应的句柄就是0, stdout是1,stderr是2。如果你windows帮助里查"重定向” 就能查到这几个数字。
第三中HANDLE是Windows 的API CreateFile返回的。
当然,最后前两种调用最后都会使用最后的CreateFile来打开文件,也就是在无论任何时候,OS HANDLE总是存在的。但是,使用HANDLE总是比使用前两种来的罗嗦,想一想Windows API那动辄十几的调用参数就够汗一把的。
为什么我们要在这几种句柄中间进行转换,我现在想到的原因有二:
1. 有些功能,比如获取文件的长度,没有对应的操作FILE*的函数能做的。函数POSIX函数_filelength就可以做到
2. 如果调用不同的函数库,不同的库可能要求的参数类型
边栏推荐
- 唐太宗把微服务的“心跳机制”玩到了极致!
- 【Try to Hack】nmap
- 第八章 习题(45A)【微机原理】【习题】
- ARM 的 缓存一致性
- Tcp/ip-- routing
- Rotating frame target detection -- about the definition and solution of rotating frame
- Transformation of DS and DXDY in surface integral of area
- 高德地图--根据地理位置获取经纬度
- 数据工程系列精讲(第三讲): Data-centric AI 之特征工程
- Document. How to use and listen for readyState
猜你喜欢
随机推荐
How to judge pure IP? Where can I find it? Is it Expensive?
Xshell比SecureCRT差吗?
HarmonyOS应用开发第二次作业笔记
Introduction and use of pytest fixture, confitest and mark
The importance of rational selection of seal clearance of hydraulic slip ring
Bit operation bit and
Pre add post add exploration and function call exploration
leetcode 279. Perfect squares (medium)
ARM 的 缓存一致性
AcWing 第 56 場周賽
Meetup03 review: introduction to the new version of linkis and the application practice of DSS
Li Kou daily question - day 24 -485 Maximum number of consecutive 1
leetcode 279. Perfect Squares 完全平方数(中等)
RISCV 的 cache
Importance of data governance
pytorch学习13:实现LetNet和学习nn.Module相关基本操作
Win10使用用户初始密码,连接Win Server失败
企业可通过4个方法提高数据库安全
Acwing game 56
View local IP address in vscode





![Sum of four numbers [array sort + double pointer]](/img/9e/e1932120a9b69847898e89c45e2eb4.png)



