Alex Rivera | Logout

Duplicating file pointers?

Asked 2011-05-23T15:24:12.120
20

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);
Edit
Report

1 Answer

16

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.

answered 2011-05-23T15:28:44.493

Your Answer