Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Copying (FILE *)s

Is there any way to copy a FILE * so that the original and new ptr can move around the opened file independantly? I don't want to have to open the file multiple times if I can avoid it because I have a wrapper class that would also be needed and it would get a little messy.


Thanks.

Rock


Comments

  • : Is there any way to copy a FILE * so that the original and new ptr can move around the opened file independantly? I don't want to have to open the file multiple times if I can avoid it because I have a wrapper class that would also be needed and it would get a little messy.


    You can pass the file pointer around to your routines as much as you like. Copies of the file pointer will work just as well as the original in the same way that copies of any value will work just as well as the original.


    However, keep in mind that if you have many copies of the file pointer lying around, and you close the file in one part of your program, it is closed to your program entirely (assuming you don't reopen it). Even if you set the file pointer to NULL somewhere, the copies will still have their original pointer value.


    However, internally, the 'current file position' is maintained probably by the file system you're using so that if one part of the program reads it will alter the 'current file position'. A subsequent read will take place at where the first read left off unless you reposition the 'current file position' with fseek (in the C world). To do a write after a read (and visa versa), you have no choice but to do an fseek, even if it doesn't change the 'current file position'.


    So I guess essentially if you have two completely independant sections of your code using a single FILE*, you have to be aware how they affect one another. (This gets even more disgusting with threads).





Sign In or Register to comment.