Solved

How to use my own defined fopen?

Posted on 2006-07-10
13
296 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
ID: 17075646
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
ID: 17076357
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
ID: 17076381
Yes - for the real fopen you need to use

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

0
 

Author Comment

by:chenzhepeter
ID: 17076457
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
ID: 17076481
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
ID: 17076567
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 86

Expert Comment

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

Author Comment

by:chenzhepeter
ID: 17077248
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
ID: 17077261
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
ID: 17078312
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
ID: 17078374
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
ID: 17078398
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
ID: 17078412
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

867 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

13 Experts available now in Live!

Get 1:1 Help Now