Override a FILE class

i need to override a few methods in FILE class so i defined few methods as
EnCrpt * fp;

fp * fopen(const char * filename, const char * mode);
int fwrite(const void * p,int length,int readLenth,FILE * fpp = NULL);
int fread(void * p,int length,int readLenth,FILE * fpp = NULL);
int fseek(FILE * fpp = NULL,long offset, int whence);
long ftell(FILE * fpp = NULL);
int feof(FILE * fpp = NULL);
int fflush(FILE * fpp = NULL);
int fclose(FILE * fpp = NULL);

i will call fread methodin my encryptedfile class .. similar to other methods..
is this correct ? can NULL file pointer create issue ?

Because i have so many place where FILE class called i don't want to change everywhere to call encryptedfile class so i am override these methods to encryptedfile class instead of standrd FILE class
This is one reason why polymorphic solutions are typically preferred to C-style structs. If FILE were polymorphic, all you'd have to do is derive your EncryptedFile class from it and it could be a drop in replacement without going back and modifying any existing code.


With C .... you don't really have that option. The only way I can think of to get around this is to macro the functions to redefine them. Example:

1
2
#define fread encryptedRead
// etc 


But that, of course, has it's own problems. For one you have to make sure those macros are defined in any source file that is using your encrypted files. For another, it makes the original fread completely inaccessible in files where that macro is defined. So you wouldn't be able to read encrypted and non-encrypted files in the same source file (at least not without undef-ing and redefining the macro multiple times)


So yeah... long story short... this situation sucks. There's no good way to do what you want. With C, you pretty much have to rename your function (which means going back and changing all your fread calls to encryptread).
i can't make it to Macro also because encryptedRead has different parameters , i mean it won't have File pointer paramter .. so i can't make it as macro ..
err... well... with a macro you can change the parameters... you just can't change the number of parameters.

If your encryptedRead function has the same parameters as fread except it takes an EncryptedFile* instead of a FILE*... then a macro will work just fine (apart from previously mentioned caveats)

But again a macro is a lousy solution. And if it won't work for you then that makes it even worse.

Unfortunately your only other option is going back and manually changing all your fread calls to something else.
how to change the function definition in macro
my EncryptedFile fread has parameter as

fread(void *p,size_t sz,size_t len)

it won't have FILE Pointer as parameter
er wait... so your EncryptedFile is a class? And fread is a member of that class?


If that's the case, you're boned. Not even a macro will work.


EDIT:

Actually... this might work:

 
#define fread(a,b,c,d)  a.read(b,c,d) 



But if the function is named 'fread' you're asking for trouble. Well you're asking for trouble anyway when doing macro substitution on a function name... but whatever.

I just want to reiterate that I really do not like the macro solution. It's a really bad idea. I only mention it because it literally is your only option apart from going back and refactoring your code (and in fact... I would even recommend you do that over this macro crap).
Last edited on
then will this override methods would work ?

EnCrpt *fp;

fp fopen(const char * filename, const char * mode);
int fwrite(const void * p,int length,int readLenth,FILE * fpp = NULL);
int fread(void * p,int length,int readLenth,FILE * fpp = NULL);
int fseek(FILE * fpp, long offset, int whence);
long ftell(FILE * fpp = NULL);
int feof(FILE * fpp = NULL);
int fflush(FILE * fpp = NULL);
int fclose(FILE * fpp = NULL);

but my concern is FILE * fpp = NULL.. will it create any issues ? its just a dummy paramter ... and fseek i can't make to NULL also bz rest i need to make NULL

I don't know what you're trying to accomplish with those functions or by giving a NULL pointer for the FILE* pointer.... but no, that won't work.

Let's back up a minute here. What is EnCrpt? A struct? A class? What are its members?
EnCrypt is class ... the functions it has most of the functions of FILE class..
fread,fwrite,feof,fgets,ftell,fflush,fclose.....

sorry my first post was wrong .. it is not pointer object...

EnCrpt fp;

fp fopen(const char * filename, const char * mode);
int fwrite(const void * p,int length,int readLenth,FILE * fpp = NULL);
int fread(void * p,int length,int readLenth,FILE * fpp = NULL);
int fseek(FILE * fpp = NULL,long offset, int whence);
long ftell(FILE * fpp = NULL);
int feof(FILE * fpp = NULL);
int fflush(FILE * fpp = NULL);
int fclose(FILE * fpp = NULL);

the fp object hold the control of whole file ... so it doesn't need a FILE pointer extra

Okay... here's the deal.

You cannot override FILE. Any code that you have written that is using FILE* will have to be refactored/changed. This is really the bottom line.


What you can do... is "trick" the compiler into making the changes for you by doing macro substitution. But again this is a bad idea... and the more I think about it... the worse I feel about it.

But if you have existing code like this:
1
2
3
4
5
void somefunc( FILE* f )
{
    fseek( f, 10, SEEK_SET );
    fread( buffer, 1, 10, f );
}


This could be "changed" to use EnCrpt instead of FILE by adding the below macros:

1
2
3
#define FILE EnCrpt
#define fseek(a,b,c) a->fseek(b,c)
#define fread(a,b,c,d) d->fread(a,b,c) 


But again... ew ew ew, you're better off just refactoring your code.
Topic archived. No new replies allowed.