C语言之fileno()函数--获取已经打开的文件的文件描述符(小技巧)
2024-08-29 15:37:30
open函数相关的: /* open 是系统调用 返回的是文件句柄*/
#include <sys/stat.h>
#include <fcntl.h> int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
fopen函数相关的: /* open是ANSIC标准中的C语言库函数,在不同的系统中应该调用不同的内核api */
FILE *fopen(const char *path, const char *mode); FILE *fdopen(int fd, const char *mode); FILE *freopen(const char *path, const char *mode, FILE *stream);
函数说明:fileno()用来取得参数stream 指定的文件流所使用的文件描述词.
void clearerr(FILE *stream); int feof(FILE *stream); int ferror(FILE *stream); int fileno(FILE *stream);
实例:
#include <stdio.h> int main(int argc, char **argv) {
FILE * fp;
int fd;
fp = fopen("/etc/passwd", "r");
fd = fileno(fp);
//等价于
//fd=open("etc/passwd",RD_ONLY);
printf("fd=%d\n", fd);
fclose(fp);
return 0;
}
最新文章
- HTML和CSS的复习总结
- oracle 触发器学习
- [Linux 性能检测工具]VMSTAT
- [No000057]一个人默默背单词,小心被传染哦
- jQuery升级踩坑大全
- 加州大学伯克利分校Stat2.3x Inference 统计推断学习笔记: FINAL
- [Html5]sessionStorage和localStorage常见操作
- Linux下安装FTP
- ios警告:Category is implementing a method which will also be implemented by its primary class 引发的相关处理
- poj 3468 A Simple Problem with Integers【线段树区间修改】
- Android APK反编译 apktool使用教程
- 利用NPOI将EXCEL转换成HTML的C#实现
- 【Android 应用开发】 ActionBar 基础
- ubuntu 18.04 配置远程ssh/远程ftp/远程vnc登陆
- Oracle XMLTYPE数据类型创建及插入
- php 解密$OOO0O0O00=__FILE__
- 使用Java创建指定大小的空文件夹
- TRIO-basic指令--MOVEMODIFY
- Effective Java 第三版——45. 明智审慎地使用Stream
- 【Go命令教程】14. go env
热门文章
- ann搜索算法(Approximate Nearest Neighbor)
- sql导入数据库
- Solaris作业管理
- Hadoop 源码阅读技巧
- [Node.js] 關於 console.log 的格式化輸出
- Mataplotlib事例操作
- git 安装及命令
- static 静态域 类域 静态方法 工厂方法 he use of the static keyword to create fields and methods that belong to the class, rather than to an instance of the class 非访问修饰符
- 使用注解来构造IoC容器-转
- MFC获取电脑硬盘序列号(附源代码)