复制文件的指针? [英] Duplicating file pointers?

查看:135
本文介绍了复制文件的指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是一个简单的问题:有没有办法复制一个文件指针(那些由 fopen()函数返回),类似于如何 DUP( )通过复制功能,如返回的文件处理程序的open()

基本上,我希望能够做这样的事:

  FILE *计划生育=的fopen(SOME_FILE,R);
FILE * FP2 = / *做一些事来复制文件指针* /;
FCLOSE(FP); // FP2仍然是开放的
/ *做一些与FP2 * /
FCLOSE(FP2);


解决方案

您可以使用的fileno 来得到一个文件中的描述符* DUP 这一点,然后用 fdopen 来获得一个新的 FILE * 在新的描述。

Just a quick question: Is there a way to duplicate a file pointer (those returned by fopen()), similar to how dup() duplicates a file handlers returned by functions like open()?

Basically, I want to be able to do something like this:

FILE *fp = fopen("some_file", "r");
FILE *fp2 = /* do something to duplicate the file pointer */;
fclose(fp); // fp2 is still open
/* do something with fp2 */
fclose(fp2);

解决方案

You could use fileno to get a descriptor for a FILE*, dup that, and then use fdopen to get a new FILE* from the new descriptor.

这篇关于复制文件的指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆