0

Once again I seek support here.

For example I have this fopen thing called fp

FILE * fp;

fp=fopen("Entity.dat","wb");

And I have x functions and there is need to use this thing in all of them, so the question is how can I make it public/global scope.

Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
undefined
  • 35
  • 7

3 Answers3

7

Ideally, you should simply pass it as a parameter to whatever functions need to use it:

void func( FILE *f, ... )
{
  // do something with f
}

int main( void )
{
  FILE *fp = fopen( "Entity.dat", "wb" );
  ...
  func( fp, ... );
  ...
}
John Bode
  • 119,563
  • 19
  • 122
  • 198
4

You should use extern:

the_file.h

#ifndef FILE_H
#define FILE_H

...

extern FILE *file;

#endif

 

the_file.cpp

#include "file.h"

FILE *file;

Then open the file and store the handle in file. Next, you can use file where do you need.

masoud
  • 55,379
  • 16
  • 141
  • 208
-1

If you have a choice, please pass it as a parameter, if you really really don't have any choice, then you could consider about singleton.

c++11

template<typename T>
class meyersSingleton
{
public:
  static T& instance()
  {
    static T theSingleton;
    return theSingleton;
  }

  meyersSingleton() = delete;
  ~meyersSingleton() = delete;
  meyersSingleton(meyersSingleton const&) = delete;
  meyersSingleton& operator=(meyersSingleton const&) = delete;

};

c++98

template<typename T>
class meyersSingleton
{
public:
  static T& instance()
  {
    static T theSingleton;
    return theSingleton;
  }

private :
  meyersSingleton();
  ~meyersSingleton();
  meyersSingleton(meyersSingleton const&);
  meyersSingleton& operator=(meyersSingleton const&);

};

you could use it as this way

#include <cstdio>

template<typename T>
class meyersSingleton
{
public:
  static T& instance()
  {
    static T theSingleton;
    return theSingleton;
  }

  meyersSingleton() = delete;
  ~meyersSingleton() = delete;
  meyersSingleton(meyersSingleton const&) = delete;
  meyersSingleton& operator=(meyersSingleton const&) = delete;

};

void read_file()
{
    char buffer [100];
    fgets (buffer , 100 , meyersSingleton<FILE*>::instance());
    printf("%s", buffer);    
}

int main()
{             
    FILE **a = &meyersSingleton<FILE*>::instance();
    *a = fopen("coeff", "rb");

    read_file();   

    return 0;
}

not a very intuitive solution, but it will guarantee you only have a global and single FILE.

If you need more single FILE, you could add one more template parameter

template<typename T, size_t N = 0>
class meyersSingleton
{
public:
  static T& instance()
  {
    static T theSingleton;
    return theSingleton;
  }

  meyersSingleton() = delete;
  ~meyersSingleton() = delete;
  meyersSingleton(meyersSingleton const&) = delete;
  meyersSingleton& operator=(meyersSingleton const&) = delete;

};

If you are using c, then just omit this solution.

Edit : In most of the cases, singleton considered as a bad solution, an anti-pattern which should no be used and leave it alone.Here are some good articles discussing about the pros and cons of singleton.

why singleton is so bad Singleton I love you, but you're bringing me down

Don't take me wrong, singleton do solve one problem--it guarantee you will only have one object(variable, resource, whatever) at a time, sometimes this is very useful(although it is rare).Log is an example, we need log everywhere, and the log wouldn't write something into our program.

Community
  • 1
  • 1
StereoMatching
  • 4,971
  • 6
  • 38
  • 70
  • I get it's not the best answer, but what's with the down votes? What happened to preferring commenting and editing over down voting? – StoryTeller - Unslander Monica Apr 22 '13 at 16:25
  • 2
    But seriously, @SteroMatching, you have to explain how using a singleton solves the OP's problem, instead of just saying how to implement a singleton. – StoryTeller - Unslander Monica Apr 22 '13 at 16:27
  • -1: Singletons are rarely needed, often abused, and in this case they add complexity for no benefit. I don't believe it is a good idea to point a beginner towards inappropriate solutions. The only suggestion I can make to improve the answer is to remove it (an alternative would be to extend it to explain why a singleton isn't a good choice here, but that would be well outside the scope of the original question). – John Bartholomew Apr 22 '13 at 19:05