开发者

How to globalize the files I'm using

开发者 https://www.devze.com 2023-03-10 06:59 出处:网络
I am using several functions that uses the same input and output files, I need to find a way to globalize them so that I could use the functions without declaring them inside the function each time it

I am using several functions that uses the same input and output files, I need to find a way to globalize them so that I could use the functions without declaring them inside the function each time its called to calirfy

void func1()
{
    ifstream infile;
    ofstream outfile;
    infile.open(" input.txt");
    outfile.open("output.txt"); 
    ......}

void func2()
{
    ifstream infile;
    ofstream outfile;
    infile.open(" input.txt");
    outfile.open("output.txt"); 
    ......}

I need to only use the dec开发者_高级运维laration files once not each time in each function, how can I do that?


you can put those functions in saperate class. what you will have to do is to create the object of class where ever you want to call the functions. this is calle reusability


Use namespace for declaring the files:

//files.h
namespace FILES
{
  extern ifstream infile;
  extern ofstream outfile;
}

And define those file objects inside a .cpp file

//files.cpp
ifstream FILES::infile ("input.txt");
ofstream FILES::outfile ("output.txt");

Usage:

void func1 ()
{
  //use FILES::infile and FILES::outfile
}
void func2 ()
{
  using namespace FILES;  // to avoid writing FILES:: always
  //use infile and outfile
}


I don't know how much I have been able to understand the problem but check out the solution. Hope this could help you.

Define the functions in a .cpp/.h file and you can use the function wherever required by writing the following line :

extern void func1(); extern void func2();

if you don't want to go with the namespace thing mentioned above. But the above answer is good.


There are as many as three types of resource that you are duplicating between your two functions: the filenames; the file streams and the contents of the files themselves. The best way to cut down this duplication and share these resources across your functions really depends on what you are doing with the streams.

Regardless, I'm not convinced that you need to globalize your data. It might be best for you to pass anything you think you'll be sharing between the two functions as parameters like so:

void func1(ifstream & infile, ofstream & outfile)
{
    ......
}
void func2(ifstream & infile, ofstream & outfile)
{
    ......
}
int main()
{
    ifstream infile;
    ofstream outfile;
    infile.open(" input.txt");
    outfile.open("output.txt"); 

    func1(infile, outfile);
    func2(infile, outfile);

    return 0;
}

Now that your main function is taking care of naming, declaring and opening files, the sub-functions can focus on their real tasks. Also, you can call func1 and func2 with files other than "input.txt" and "output.txt" without needing to make edits to the functions themselves. Having the objects exist at function scope - instead of globally - also makes it easier to keep track of what is going on because it is more clear what code is working on what data.

(Note that because the lifetime of the streams spans func1 and func2, things that you do to them in one will have an effect in the other. In particular, the seek positions of the streams will persist. This would not be the case if you opened them afresh in each sub-function or passed in copies of the streams by omitting the &'s.)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号