使用freopen()打印到文件和屏幕

时间:2012-12-20 18:43:05

标签: c++ freopen

我正在尝试使用freopen()打印到文本文件和屏幕,但我只是打印到文件。

我想知道是否可以轻松将程序输出保存到文件并将其打印到屏幕上?因为我以另一种方式工作,但最终我不得不打印两次声明。一个用于文件,另一个用于输出。

注意:我是C ++的新手,我正在尝试为下一学期的课程学习它,因此我需要直接回答,因为我已经在线查看,除此之外找不到任何简单的答案。

这是我到目前为止所做的:

#include<iostream>
#include<time.h>
#include<stdlib.h>
#include<fstream>

using namespace std; 

void menu(){
    cout << "\t********************************************************\n"
         << "\t* Welcome to slot machine.                             *\n"
         << "\t* Would you like to play? (1 to play, 2 not to play)   *\n"
         << "\t********************************************************\n\n";
    return;
}

void update(int arr[], int &token) {
    if (arr[0]==arr[1] && arr[1]==arr[2]) {
        token+=4;
        cout << "You win\n\n";
    } else if (arr[0]==arr[1] || arr[1]==arr[2] || arr[0]==arr[2]) {
        token+=1;
        cout << "You got two out of three\n\n";
    } else {
        token-=1;
        cout << "You lose\n\n";
    }
}

int main() {
    freopen("file.txt", "w", stdout);
    int x, arr[3], token=4;
    srand(time(0));
    menu();
    cin >> x;
    while(token!=0) {
        cout << "You have " << token << " tokens\n\n"
             << "Pull? (1 to pull, 2 not to pull)\n\n";
        cin>>x;
        if(x==1) {
            for(int i=0; i<3; i++) {
                arr[i]=1+rand()%10;
            }
            cout << "\t\t";
            for(int j=0; j<3; j++) {
                cout << arr[j] << " ";
            }
            cout << "\n\n";
            update(arr,token);
        }
        else{  
            cout << "OK\n";
        }
    }
    cin.get();
    return 0;
}

2 个答案:

答案 0 :(得分:6)

我不知道实现这一目标的简单方法,但我已设法以某种方式解决这个问题。

使用fstreams,您可以按照写入控制台的相同方式输出文件。

#include <fstream>

int main()
{
     std::ofstream f("file.txt");
     f << "something";
}

现在我们可以开始了:有没有办法可以同时输出到控制台和文件?

我最近编写了流解复用器来解决这个问题:

#include <vector>
#include <ostream>
class stream_demultiplexer
{
private:
    typedef std::vector<std::ostream*> str_cont;
    str_cont d;
public:
    stream_demultiplexer& put(std::ostream::char_type ch)
    {
        for(str_cont::iterator it = d.begin(); it != d.end(); ++it)
            (*it)->put(ch);
        return *this;
    }

    stream_demultiplexer& write(const std::ostream::char_type* s, std::streamsize count)
    {
        for(str_cont::iterator it = d.begin(); it != d.end(); ++it)
            (*it)->write(s, count);
        return *this;
    }

    stream_demultiplexer& flush()
    {
        for(str_cont::iterator it = d.begin(); it != d.end(); ++it)
            (*it)->flush();
        return *this;
    }


    template<typename T>
    stream_demultiplexer& operator<<( const T& obj )
    {
        for(str_cont::iterator it = d.begin(); it != d.end(); ++it)
            (**it) << obj;
        return *this;
    }

    stream_demultiplexer& operator<<(std::ios_base& (*func)(std::ios_base&))
    {
        for(str_cont::iterator it = d.begin(); it != d.end(); ++it)
            (**it) << func;
        return *this;
    }

    template<typename CharT, typename Traits>
    stream_demultiplexer& operator<<(std::basic_ios<CharT,Traits>& (*func)(std::basic_ios<CharT,Traits>&) )
    {
        for(str_cont::iterator it = d.begin(); it != d.end(); ++it)
            (**it) << func;
        return *this;
    }

    stream_demultiplexer& operator<<(std::ostream& (*func)(std::ostream&) )
    {
        for(str_cont::iterator it = d.begin(); it != d.end(); ++it)
            (**it) << func;
        return *this;
    }

    void add_stream(std::ostream& ss)
    {
        d.push_back(&ss);
    }
};

你可以像这样使用它:

stream_demultiplexer spl;
std::ofstream f("file.txt");
spl.add_stream(f);
spl.add_stream(std::cout);
spl << 55 << " HELLO WORLD";

我的方法的优点是操纵器和无格式输出正常工作:

spl << 76 << " " << std::hex << 76 << std::endl;
spl.put('a');
spl.write("ABCDE", 5);

答案 1 :(得分:4)

类似UNIX的环境中的简单方法是使用shell命令tee

$ my-program | tee output.txt

会将stdout复制到终端,也会复制到文件output.txt


如果您必须在代码中执行此操作,则可以使用自己的输出流而不是cout,它会将每operator<<转发到两个(或更多)ostreams。这比使用C ++ ostream cout下面的C输出文件更好(对我而言)。

#include <ostream>

class Tee {
    std::ostream &first, &second;

    template<typename T> friend Tee& operator<< (Tee&, T);

public:
    Tee(std::ostream &f, std::ostream &s) : first(f), second(s) {}
};

template <typename T>
Tee& operator<< (Tee &t, T val)
{
    t.first << val;
    t.second << val;
    return t;
}

然后,如果用以下代码替换你的freopen行:

std::ofstream outfile("file.txt");
Tee tee(std::cout, outfile);

您可以使用tee <<代替cout <<

请注意,您需要将tee传递到您的函数中,或者将其设置为全局函数。