使用freopen()打印到文件和屏幕 [英] Using freopen() to print to file and screen

查看:216
本文介绍了使用freopen()打印到文件和屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用freopen()打印到文本文件和屏幕,但我只是实现打印到一个文件。

I am trying to use freopen() to print to a text file and the screen, but I am only achieving the printing to a file.

我想知道如果有一个易于保存的程序输出到一个文件并打印到屏幕?因为我有另一种方式工作,但我最终必须打印出每一个语句两次。一个是为文件,另一个只是为了输出。

I was wondering if there was an easy to save the programs output to a file and print it to the screen? Because I had this working another way, but I ended up having to print out every statement twice. One being for the file the other just for the output.

注意:我是新的C ++,我试图学习它下一学期的类,所以直接回答需要的,因为我已经看了网上,找不到任何简单的答案这个解决方案除了。

Note: I am new to C++ and I am trying to learn it for a class next semester so direct answer are needed as I have already look online and couldn't find any simple answers to this solution besides.

这是我到目前为止:

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


推荐答案

使用fstreams,你可以以相同的方式输出到文件,你可以写到控制台。

Using fstreams you can output to file the same way you can write to console.

#include <fstream>

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

现在有一点我们可以开始:

Now there's a point we can start: is there a way we can output to the console and file simultaneously?

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

I've recently written stream demultiplexer to address that problem:

#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";

我的方法有优势,操纵器和未格式化的输出可以正常工作:

My approach has advantage that manipulators and unformatted output works correctly:

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

这篇关于使用freopen()打印到文件和屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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