Solved

How to use my own defined fopen?

Posted on 2006-07-10
13
295 Views
Last Modified: 2012-05-05
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.
0
Comment
Question by:chenzhepeter
13 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 125 total points
Comment Utility
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
 

Author Comment

by:chenzhepeter
Comment Utility
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
 
LVL 11

Assisted Solution

by:cup
cup earned 75 total points
Comment Utility
Yes - for the real fopen you need to use

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

0
 

Author Comment

by:chenzhepeter
Comment Utility
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
 

Author Comment

by:chenzhepeter
Comment Utility
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
 

Author Comment

by:chenzhepeter
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 86

Expert Comment

by:jkr
Comment Utility
Could you post both the declaration and how you're using it?
0
 

Author Comment

by:chenzhepeter
Comment Utility
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
 
LVL 11

Assisted Solution

by:dbkruger
dbkruger earned 50 total points
Comment Utility
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
 

Author Comment

by:chenzhepeter
Comment Utility
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
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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
 

Author Comment

by:chenzhepeter
Comment Utility
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
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now