文件指针/句柄(FILE*)、文件描述符(fd)以及 文件路径(filepath)的相互转换(完整版,收集,整理)
2011-11-24 14:53
495 查看
定义
1、pathname --> fp,fd
2、fd --> fp,pathname
下面这个函数能够实现由fd转成pathname,
3、fp --> fd,pathname
由于有get_pathname_from_fd作基础,很容易实现fp->pathname,
FILE *fp; //文件指针、句柄 int fd; //文件描述符 char pathname[1024]; //文件路径
1、pathname --> fp,fd
fp = fopen(pathname,"r");
fd = open(pathname,O_RDONLY);
2、fd --> fp,pathname
fp = fdopen(fd,"r");
下面这个函数能够实现由fd转成pathname,
int get_pathname_from_fd(int fd, char pathname[], int n) { char buf[1024]; pid_t pid; bzero(buf, sizeof(buf)); pid = getpid(); snprintf(buf, sizeof(buf), "/proc/%i/fd/%i", pid, fd); return readlink(buf, pathname, n); }
3、fp --> fd,pathname
fd = fileno(fp);
由于有get_pathname_from_fd作基础,很容易实现fp->pathname,
int get_pathname_from_fp(FILE *fp, char pathname[], int n)
{
return get_pathname_from_fd(fileno(fp),pathname,n);
}
相关文章推荐
- 文件指针/句柄(FILE*)、文件描述符(fd)以及 文件路径(filepath)的相互转换(完整版,收集,整理)
- 文件指针/句柄(FILE*)、文件描述符(fd)以及 文件路径(filepath)的相互转换
- 文件指针/句柄(FILE*)、文件描述符(fd)以及 文件路径(filepath)的相互转换
- 文件指针/句柄(FILE*)、文件描述符(fd)以及 文件路径(filepath)的相互转换
- 文件指针/句柄(FILE*)、文件描述符(fd)以及 文件路径(filepath)的相互转换
- 文件指针/句柄(FILE*)、文件描述符(fd)以及 文件路径(filepath)的相互转换
- 文件指针/句柄(FILE*)、文件描述符(fd)以及 文件路径(filepath)的相互转换
- 文件指针/句柄(FILE*)、文件描述符(fd)以及 文件路径(filepath)的相互转换
- Linux C语言 文件指针/句柄(FILE*)、文件描述符(fd)以及 文件路径(filepath)的相互转换
- 文件指针/句柄(FILE*)、文件描述符(fd)以及 文件路径(filepath)的相互转换
- 文件指针/句柄(FILE*)、文件描述符(fd)以及 文件路径(filepath)的相互转换
- 文件指针/句柄(FILE*)、文件描述符(fd)以及 文件路径(filepath)的相互转换(转)
- 文件指针/句柄、文件描述符、文件路径的相互转换
- Android中文件File path与content:// Uri的相互转换
- Linux中文件描述符fd与文件指针FILE*互相转换实例解析
- C# Path操作路径文件、File处理文件、读写文件:byte[];string[];string; string与byte[]转换,File.AppendAllText()
- 文件描述符fd和文件指针fp之间的相互转换
- 文件指针和文件描述符之间的相互转换 fd---->fp 和 fp---->fd
- 文件File path与content:// Uri的相互转换
- android Uri利用及解析(文件操作)以及与路径的相互转换