是否有一个很好的习惯用语来处理替代输出流?

时间:2016-07-19 09:46:11

标签: c++ iostream ostream

我想编写一个简单的程序,根据传递的选项,可执行文件会将输出打印到屏幕或文件中。该计划很简单。

#include<iostream>
int main(int argc, char* argv[]){
    ... process options...

    std::ostream& out = ... // maybe std::cout, maybe a *new* std::ofstream;
    out << "content\n";
}

是否有一个很好的习惯用法可以在运行时替代std::cout或文件流?

我尝试了指针,但它太可怕了。我无法避免使用指针(更不用说稍后需要更难看的代码来删除指针)。

#include<iostream>
#include<ofstream>
int main(int argc, char* argv[]){

    std::string file = argc>1?argv[1]:"";
    std::clog << "file: " << file << '\n';
    // if there is no argument it will print to screen
    std::ostream* out = (file=="")?&std::cout:(new std::ofstream(file)); // horrible code
    *out << "content" << std::endl;
    if(out != &std::cout) delete out;

}

我不知道,也许C ++流的某些功能允许这样做。也许我必须使用某种类型的擦除。我认为问题在于std::cout已经存在的东西(是全局的),但std::ofstream是必须要创建的东西。

我设法使用open并避免使用指针,但它仍然很难看:

int main(int argc, char* argv[]){

    std::string file = argc>1?argv[1]:"";
    std::clog << "file: " << file << '\n';

    std::ofstream ofs; 
    if(file != "") ofs.open(file); 
    std::ostream& out = (file=="")?std::cout:ofs;
    out << "content" << std::endl;
}

6 个答案:

答案 0 :(得分:5)

过度工程化。

#include <iostream>
#include <fstream>

int main(int argc, char* argv[]) {

    std::ofstream ofs(argc > 1 ? argv[1] : "");
    std::ostream& os = ofs.is_open() ? ofs : std::cout;

    // use os ...
}

答案 1 :(得分:5)

我的偏好是使用安装了合适的流缓冲区的流。以下是直接输出到文件或std::cout的一种方式:

#include <iostream>
#include <fstream>
int main(int ac, char* av) {
    std::ofstream ofs;
    if (1 < ac) {
       ofs.open(av[1]);
       // handle errors opening the file here
    }
    std::ostream os(file? file.rdbuf(): std::cout.rdbuf());
    // use os ...
}

答案 2 :(得分:2)

所需流的运行时绑定几乎需要看起来像你已经拥有的那样。

关于指针问题,确定你可以清理一下......也许是这样的?这假设您只想在参数存在的情况下创建ofstream

int main(int argc, char* argv[]){ 
    std::string file = argc > 1 ? argv[1] : "";
    std::clog << "file: " << file << '\n';

    // if there is no argument it will print to screen
    std::unique_ptr<std::ostream> fp;
    if (file == "")
        fp = std::make_unique<std::ofstream>(file);

    std::ostream& out = (fp && fp->is_open()) ? std::cout : *fp; // not so horrible code

    out << "content" << std::endl;
}

如果不需要动态对象,最简单的可能就是列出这个;

int main(int argc, char* argv[]){
    std::string filename = (argc > 1) ? argv[1] : "";

    std::ofstream file(filename);

    // if there is no argument (file) it will print to screen
    std::ostream& out = file.is_open() ? file : std::cout;

    out << "content" << std::endl;
}

答案 3 :(得分:1)

您可以使用指向流的共享指针来获取多态行为:

#include <memory>
#include <fstream>
#include <sstream>
#include <iostream>

void nodelete(void*) {}

std::shared_ptr<std::ostream> out_screen_stream() { return std::shared_ptr<std::ostream>(&std::cout, nodelete); }
std::shared_ptr<std::ostream> out_file_stream() { return std::make_shared<std::ofstream>(); }
std::shared_ptr<std::ostream> out_string_stream() { return std::make_shared<std::ostringstream>(); }

int main ()
{
    std::shared_ptr<std::ostream> out;

    // case condition:
    out = out_screen_stream();
    out = out_file_stream();
    out = out_string_stream();

    *out << "content" << std::endl;
    return 0;
}

注意:std :: shared_ptr允许管理不同的可能流,其中某些流不应被删除(例如:std :: cout)。

类似,但使用std :: unique_ptr:

#include <memory>
#include <fstream>
#include <sstream>
#include <iostream>

class Deleter
{
    public:
    Deleter(bool use_delete = true) : use_delete(use_delete) {}

    template <typename T>
    void operator () (const T* p) {
        if(use_delete)
            delete p;
    }

    bool nodelete() const { return ! use_delete; }

    private:
    bool use_delete;
};

using unique_ostream_ptr = std::unique_ptr<std::ostream, Deleter>;
unique_ostream_ptr out_screen_stream() { return unique_ostream_ptr(&std::cout, false); }
unique_ostream_ptr out_file_stream() { return unique_ostream_ptr{ new std::ofstream }; }
unique_ostream_ptr out_string_stream() { return unique_ostream_ptr{ new std::ostringstream  }; }

int main ()
{
    unique_ostream_ptr out;

    // case condition:
    out = out_screen_stream();
    out = out_file_stream();
    out = out_string_stream();

    *out << "content" << std::endl;
    return 0;
}

答案 4 :(得分:1)

我经常在命令行工具中使用这样的东西:

int main(int, char* argv[])
{
    std::string filename;

    // args processing ... set filename from command line if present
    if(argv[1])
        filename = argv[1];

    std::ofstream ofs;

    // if a filename was given try to open
    if(!filename.empty())
        ofs.open(filename);

    // bad ofs means tried to open but failed
    if(!ofs)
    {
        std::cerr << "Error opeing file: " << filename << '\n';
        return EXIT_FAILURE;
    }

    // Here either ofs is open or a filename was not provided (use std::cout)

    std::ostream& os = ofs.is_open() ? ofs : std::cout;

    // write to output
    os << "Some stuff" << '\n';

    return EXIT_SUCCESS;
}

答案 5 :(得分:-4)

也许是参考?

#include<iostream>
#include<ofstream>


int main(int argc, char* argv[])
{
    auto &out = std::cout;
    std::ofstream outFile;

    std::string fileName = argc>1?argv[1]:"";

    std::clog << "file: " << file << '\n';
    // if there is no argument it will print to screen
    if(!fileName.empty())
    {
        outFile.open(fileName);
        out = outFile;
    }

    out<<"one, one, two";

    return 0;
}
相关问题