Link to home
Start Free TrialLog in
Avatar of chenzhepeter
chenzhepeter

asked on

How to use my own defined fopen?

I am writing a C++ program in which I need to define my own version of fopen,fread,fwrite and fclose, fseek. I have already done this. Now I want to use these functions in my program but the compiler told me they are multiple defined: They have the same function name and arguments as the ones in the standard library.

How do I overwrite these functions in standard library? Do I need to use inheritance? And how to do this?

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of chenzhepeter
chenzhepeter

ASKER

can I put my function inside
namespace mystdio {

// declarations go here

};

and type

using namespace mystdio

in the .cpp file I am using fopen. Does this solve the name conflicts?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
it seems I have to use mystdio::fopen() to call my defined fopen function. Is there a way I just call fopen and the compiler knows I am calling my own fopen?
I got this error:

"test\\test_audio.cpp", line 717: error: more than one instance of overloaded
          function "fopen" matches the argument list:
            function "fopen(const char *, const char *) C"
            function "mystdio::fopen(const char *, const char *)"
            argument types are: (char [64], const char [3])
      data.tty.fpUlOut = fopen(fname, "wb");
I am trying out   using mystdio::fopen;

but I got this error"

"test\\test_audio.cpp", line 23: error: using-declaration of function
          "mystdio::fopen(const char *, const char *)" conflicts with function
          "fopen(const char *, const char *) C" (declared at line 84 of
          "C:\proj\vendor\intel\nordheim\\Msa\Msa\include\stdio.h")
Could you post both the declaration and how you're using it?
In mystdio.h

namespace mystdio {

void setbuf(FILE *, char *);

FILE* fopen(const char* name, const char* mods);

size_t fread(void* bufptr, size_t size, size_t count, FILE* str);

size_t fwrite(const void *bufptr, size_t size, size_t count, FILE *str);

int fseek(FILE *stream, long offset, int origin);

int fclose( FILE* str);

}

In mystdio.cpp

#include "mystdio.h"

namespace mystdio {
      
FILE* fopen(const char* name, const char* mods){
      
      unsigned int fid = 0;
   
      unsigned char first =1;
      
      char filemod[2];
      char filename[30];      
      
      memset(filemod, 0, LENGTH(filemod));
      memset(filename, 0, LENGTH(filename));      

      strcat(filemod,mods);

      strcat(filename,name);

      testifwrite(&first); //special command

      testifwriteV(filemod,LENGTH(filemod));

      testifwriteV(filename, LENGTH(filename));
      
      testifflush();
      
      testifread(&fid);
      
      FILE * fptr= (FILE*)fid;

      return fptr;
}
...//followed by other fread,fwrite..
}


In test.cpp

#include "mystdio.h"
using namespace mystdio;

void VectorLoad(TestIFClass &testif) {

    data = fopen(fname, "rb");
....
}


It works if the function is name as fopen1,fread1, but I am trying to change them into fopen,fread so other people don't need to change their code.

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I am not actually dealing with file I/O in C++, I just pretend my function as normal fopen,fread, but what they actually do is sending message to Matlab to do the file I/O actions.

The reason I have to use function name as fopen,fread is solely because that's how other programmers call them in the program. Other programmers would think they are calling standard file I/O to open and read a file but actually in the back, my code is calling Matlab to do same special actions.

Still thanks for your kind remaind, dbkruger~
Well, the 'namespace' seems to be missing a semicolon:

namespace mystdio {

void setbuf(FILE *, char *);

FILE* fopen(const char* name, const char* mods);

size_t fread(void* bufptr, size_t size, size_t count, FILE* str);

size_t fwrite(const void *bufptr, size_t size, size_t count, FILE *str);

int fseek(FILE *stream, long offset, int origin);

int fclose( FILE* str);

}; // <--
Ok, it works now, I found some other header include stdio.h and this cause two namespace declared.

jkr: I found without semicolon is working too. Are you sure it's required?
Hey, it's early in the morning here, if it works without it, that's fine - a shot in the dark that has been proven wrong, as well as myself here...