文件和控制台输出的争论 [英] arguement for file as well as console output

查看:63
本文介绍了文件和控制台输出的争论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个功能,可以将大量信息打印到控制台以及文件中.

因此,它看起来像这样:

void display()
{

ofstream fout;
fout.open("D:\\Output.txt");

cout << "Value of x="<<x<<endl;
cout << "Value of y="<<y<<endl;
cout << "Value of z="<<z<<endl;

fout << "Value of x="<<x<<endl;
fout << "Value of y="<<y<<endl;
fout << "Value of z="<<z<<endl;
}



问题是,我必须将所有cout行重复到fout,以便在控制台以及文件中打印相同的行.我们可以提供类似这样的功能吗?

void display(param)
{
param << "Value of x="<<x<<endl;
param << "Value of y="<<y<<endl;
papam << "Value of z="<<z<<endl;
}


然后使用coutfout争论两次调用此函数:

display(cout);
display(fout);



这样代码看起来就不那么笨拙了.

请帮助我...

解决方案

我认为,如果将输出字符串传递给输出设备而不是将输出设备传递给Display函数,那会更好.

因此,您的显示器将看起来更像

  void 显示(参数)
{
  cout<<参数<< endl;
  fout<<参数<< endl;
}


如果要保持相同的接口,可以制作伪造的流(像流一样支持<<<运算符的类).


  class  dupstream
{
    std :: ostream * s1,* s2;
    dupstream(comnst dupstream&);
    上游& 运算符 =(复合dupstream&);
公共:
    dupstream(std :: ostream& a,std :: ostream& b)
        :s1(& a),s2(& b)
    {}
    模板< T类>
    朋友 dupstream&运算符<(dupstream& s, const  T& a)
    {
        * s1<<一个;
        * s2<<一个;
        返回 s;
    }
}; 


然后将其用作

信息流;
fout.open(" );
dupstream(std :: cout,fout);

上游<< " <<< x<< endl;的值;
上游<< " << y<< endl;的值;
上游<< " <<< z<< endl; 


如果要将输出流作为函数参数传递,可以这样:

void myfunction(ostream& outs)
{
	outs << "hello";
}



但是,亨利·明特(Henry Minute)的方法似乎是一种更好的解决方案.

I have a function that prints large amount of information to the console as well as to a file.

As such, it looks something like this:

void display()
{

ofstream fout;
fout.open("D:\\Output.txt");

cout << "Value of x="<<x<<endl;
cout << "Value of y="<<y<<endl;
cout << "Value of z="<<z<<endl;

fout << "Value of x="<<x<<endl;
fout << "Value of y="<<y<<endl;
fout << "Value of z="<<z<<endl;
}



The problem is, I have to repeat all the cout lines to fout for the same lines to be printed in console as well as file. Can we have a function something like this:

void display(param)
{
param << "Value of x="<<x<<endl;
param << "Value of y="<<y<<endl;
papam << "Value of z="<<z<<endl;
}


and then call this function twice with cout and fout arguements:

display(cout);
display(fout);



so that the code looks far less clumsy..

please help me out...

解决方案

I think that it would be better if, rather than passing the output device to your Display function, pass it the output string.

So your Display would then look more like

void Display(param)
{
  cout << param<<endl;
  fout << param<<endl;
}


If you want to maintain the same interface, you can make a fake stream (a class that support the << operator like a stream) that redirect the output.

Something like

class dupstream
{
    std::ostream *s1, *s2;
    dupstream(comnst dupstream&);
    dupstream& operator=(comnst dupstream&);
public:
    dupstream(std::ostream& a, std::ostream& b)
        :s1(&a), s2(&b)
    {}
    template<class T>
    friend dupstream& operator<<(dupstream& s, const T& a)
    {
        *s1 << a;
        *s2 << a;
        return s;
    }
};


and then use it as

ofstream fout;
fout.open("D:\\Output.txt");
dupstream(std::cout, fout);

dupstream << "Value of x="<<x<<endl;
dupstream << "Value of y="<<y<<endl;
dupstream << "Value of z="<<z<<endl;


If you want to pass an output stream as function argument you could do that like so:

void myfunction(ostream& outs)
{
	outs << "hello";
}



Henry Minute''s way seems like the nicer solution however.


这篇关于文件和控制台输出的争论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆