?
Solved

How to use my own defined fopen?

Posted on 2006-07-10
13
Medium Priority
?
307 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
13 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 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 300 total points
ID: 17076381
Yes - for the real fopen you need to use

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

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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
 
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 200 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

752 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