如何全球化我正在使用的文件

发布于 2024-11-13 07:11:36 字数 418 浏览 5 评论 0原文

我正在使用多个使用相同输入和输出文件的函数,我需要找到一种方法来全球化它们,以便我可以使用这些函数,而无需每次调用​​时在函数内声明它们 要校准,

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 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 declaration files once not each time in each function, how can I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一笑百媚生 2024-11-20 07:11:36

您可以将这些函数放在单独的类中。你所要做的就是在你想要调用函数的地方创建类的对象。这就是所谓的可重用性

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

小梨窩很甜 2024-11-20 07:11:36

使用 namespace 声明文件:

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

并在 .cpp 文件内定义这些文件对象

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

用法:

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

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
}
溺孤伤于心 2024-11-20 07:11:36

我不知道我对这个问题的理解有多少,但请查看解决方案。希望这可以帮助你。

在 .cpp/.h 文件中定义函数,您可以在任何需要的地方通过编写以下行来使用该函数:

extern void func1();
外部无效 func2();

如果您不想使用上面提到的名称空间。但上面的答案很好。

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.

温柔嚣张 2024-11-20 07:11:36

您在两个函数之间复制了多达三种类型的资源:文件名;文件流和文件本身的内容。减少重复并在各个功能之间共享这些资源的最佳方法实际上取决于您对流的处理方式。

无论如何,我不相信您需要全球化数据。您最好将您认为将在两个函数之间共享的任何内容作为参数传递,如下所示:

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;
}

现在您的 main 函数正在负责命名、声明和打开文件,子函数- 职能部门可以专注于他们的实际任务。此外,您还可以使用“input.txt”和“output.txt”以外的文件调用 func1func2,而无需对函数本身进行编辑。让对象存在于函数范围内(而不是全局)也可以更轻松地跟踪正在发生的事情,因为更清楚哪些代码正在处理哪些数据。

(请注意,由于流的生命周期跨越 func1func2,因此您在一个流中对它们所做的操作将会对另一个流产生影响。特别是,查找位置如果您在每个子函数中重新打开它们或通过省略 & 传入流的副本,则情况不会如此。)

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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文