Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicating file pointers?

Tags:

c

file-io

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);
like image 295
Frxstrem Avatar asked May 23 '11 15:05

Frxstrem


1 Answers

FILE *fp2 = fdopen (dup (fileno (fp)), "r");
like image 127
Gonzalo Avatar answered Oct 05 '22 01:10

Gonzalo