赞
踩
定义
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);
}
转载于:https://www.cnblogs.com/niocai/archive/2011/11/24/2261686.html
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。