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.
chenzhepeterAsked:
Who is Participating?
 
jkrConnect With a Mentor Commented:
The easiest way - apart from naming them differently, which is obvious - *could* be to put them in a separate namespace, e.g.

namespace mystdio {

// declarations go here

};

and explicitly referring to them as

FILE* p = mystdio::fopen("file.txt","r");

etc.

Inheritance is not an issue for functions.
0
 
chenzhepeterAuthor Commented:
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?
0
 
cupConnect With a Mentor Commented:
Yes - for the real fopen you need to use

FILE* p = ::fopen (...)

0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
chenzhepeterAuthor Commented:
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?
0
 
chenzhepeterAuthor Commented:
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");
0
 
chenzhepeterAuthor Commented:
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")
0
 
jkrCommented:
Could you post both the declaration and how you're using it?
0
 
chenzhepeterAuthor Commented:
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.

0
 
dbkrugerConnect With a Mentor Commented:
I'm sure the other posters here will answer your question in the end, but this is not a very good idea. If your'e writing new code, then you shouldn't be using fopen. The API is slow, not compile-time type-safe. If you want slow and convenient, use ofstream. If you want fast, write your own using open, read, write, etc.
0
 
chenzhepeterAuthor Commented:
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~
0
 
jkrCommented:
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);

}; // <--
0
 
chenzhepeterAuthor Commented:
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?
0
 
jkrCommented:
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...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.