C++ “cin”的通用接口和“文件”

发布于 2024-09-25 07:32:42 字数 270 浏览 1 评论 0原文

cin 和 file 输入有通用的接口吗?

我想制作一个具有可选参数的程序

prog [input-file]

如果指定了输入文件,那么它应该从该文件读取,如果没有,它应该从 cin 读取。

据我所知,它们都实现了 istream。你会如何设置它,以便我可以在 >> 中执行类似 的操作? var,其中 in 是一个 istream

Is there a common interface for cin and file input?

I want to make a program that has an optional parameter

prog [input-file]

If an input file is specified, then it should read from the file, and if not, it should read from cin.

From what I can tell, they both implement istream. How would you set up it so that I could do something like in >> var, where in is an istream.

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

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

发布评论

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

评论(4

就是爱搞怪 2024-10-02 07:32:42
#include <iostream>
#include <fstream>

int main(int argc, char **argv)
{
    std::ifstream f;
    if (argc >= 2) {
        f.open(argv[1]);
    }
    std::istream &in = (argc >= 2) ? f : std::cin;

    // use in here
}

您可以将其中一些工作转移到帮助程序类中,以更清楚地了解发生了什么(请注意,在无法打开文件的情况下,其行为略有不同):

#include <iostream>
#include <fstream>

class ifstream_or_cin_t {
    std::ifstream f;

public:
    ifstream_or_cin_t(const char *filename)
    {
        if (filename) {
            f.open(filename);
        }
    }

    operator std::istream &() { return f.is_open() ? f : std::cin; }
};

static void do_input(std::istream &in)
{
    // use in...
}

int main(int argc, char **argv)
{
    do_input(
        ifstream_or_cin_t((argc >= 2) ? argv[1] : NULL));
}
#include <iostream>
#include <fstream>

int main(int argc, char **argv)
{
    std::ifstream f;
    if (argc >= 2) {
        f.open(argv[1]);
    }
    std::istream &in = (argc >= 2) ? f : std::cin;

    // use in here
}

You could shift some of this work into a helper class to make it clearer what's going on (note that this has slightly different behavior in the case where the file can't be opened):

#include <iostream>
#include <fstream>

class ifstream_or_cin_t {
    std::ifstream f;

public:
    ifstream_or_cin_t(const char *filename)
    {
        if (filename) {
            f.open(filename);
        }
    }

    operator std::istream &() { return f.is_open() ? f : std::cin; }
};

static void do_input(std::istream &in)
{
    // use in...
}

int main(int argc, char **argv)
{
    do_input(
        ifstream_or_cin_t((argc >= 2) ? argv[1] : NULL));
}
ゝ杯具 2024-10-02 07:32:42

您可以编写一个引用 std::istream 的函数:

void do_input(std::istream& the_istream)
{
    int my_awesome_variable;
    the_istream >> my_awesome_variable;
}

用法示例:

int main()
{
    // reading from stdin:
    do_input(std::cin);

    // reading from a file:
    std::ifstream fs("test.txt");
    do_input(fs);
}

You can write a function that takes a reference to an std::istream:

void do_input(std::istream& the_istream)
{
    int my_awesome_variable;
    the_istream >> my_awesome_variable;
}

Usage example:

int main()
{
    // reading from stdin:
    do_input(std::cin);

    // reading from a file:
    std::ifstream fs("test.txt");
    do_input(fs);
}
苏别ゝ 2024-10-02 07:32:42

我的2P价值:

#include <iostream>
#include <fstream>
#include <string>

extern char const* findFlag(int,char*[],char const*);
int main(int argc,char* argv[])
{
    std::string     isFile  = findFlag(argc,argv,"-f");
    std::ifstream   file;

    if (isFile != "")
    {   file.open(isFile.c_str());
    }
    std::istream&   data    = isFile != "" ? file : std::cin;


}

My 2P worth:

#include <iostream>
#include <fstream>
#include <string>

extern char const* findFlag(int,char*[],char const*);
int main(int argc,char* argv[])
{
    std::string     isFile  = findFlag(argc,argv,"-f");
    std::ifstream   file;

    if (isFile != "")
    {   file.open(isFile.c_str());
    }
    std::istream&   data    = isFile != "" ? file : std::cin;


}
素年丶 2024-10-02 07:32:42
istream& input = cin;

或者

inputFileStream ifstream(fileName);
istream& input = inputFileStream;

但是我认为这不是一个很好的方法,因为 cin 没有 close() 而 ifstream 有。

istream& input = cin;

or

inputFileStream ifstream(fileName);
istream& input = inputFileStream;

However I think that this is not a very good method since cin doesn't have a close() whereas ifstream does.

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