从 cin 或文件读取

发布于 2024-08-23 23:27:11 字数 215 浏览 3 评论 0原文

当我尝试编译代码时,

istream in;
if (argc==1)
        in=cin;
else
{
        ifstream ifn(argv[1]);
        in=ifn;
}

gcc 失败,抱怨 operator= 是私有的。有没有办法根据条件将 istream 设置为不同的值?

When I try to compile the code

istream in;
if (argc==1)
        in=cin;
else
{
        ifstream ifn(argv[1]);
        in=ifn;
}

gcc fails, complaining that operator= is private. Is there any way to set an istream to different values based on a condition?

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

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

发布评论

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

评论(4

反目相谮 2024-08-30 23:27:11

您可以使用 in 指针,例如:

istream *in;
ifstream ifn;

if (argc==1) {
     in=&cin;
} else {
     ifn.open(argv[1]);
     in=&ifn;
}

You could use a pointer for in, e.g.:

istream *in;
ifstream ifn;

if (argc==1) {
     in=&cin;
} else {
     ifn.open(argv[1]);
     in=&ifn;
}
眼泪淡了忧伤 2024-08-30 23:27:11

您可以用另一个替换 cin 的streambuf,在某些程序中,这比传递 istreams 而不直接引用 cin 的一般策略更简单。

int main(int argc, char* argv[]) {
  ifstream input;
  streambuf* orig_cin = 0;
  if (argc >= 2) {
    input.open(argv[1]);
    if (!input) return 1;
    orig_cin = cin.rdbuf(input.rdbuf());
    cin.tie(0); // tied to cout by default
  }

  try {
    // normal program using cin
  }
  catch (...) {
    if (orig_cin) cin.rdbuf(orig_cin);
    throw;
  }

  return 0;
}

尽管在控制离开 main 后使用 cin 的情况极为罕见,但如果您的程序可能会执行此操作,则上述 try-catch 可以避免未定义的行为。

You can replace cin's streambuf with another, and in some programs this is simpler than the general strategy of passing around istreams without referring to cin directly.

int main(int argc, char* argv[]) {
  ifstream input;
  streambuf* orig_cin = 0;
  if (argc >= 2) {
    input.open(argv[1]);
    if (!input) return 1;
    orig_cin = cin.rdbuf(input.rdbuf());
    cin.tie(0); // tied to cout by default
  }

  try {
    // normal program using cin
  }
  catch (...) {
    if (orig_cin) cin.rdbuf(orig_cin);
    throw;
  }

  return 0;
}

Even though it's extremely rare to use cin after control leaves main, the above try-catch avoids undefined behavior if that's something your program might do.

枕梦 2024-08-30 23:27:11

那么,它不是在抱怨“没有合适的构造函数可用”吗?无论如何,您可以按如下方式修改它。

void Read(istream& is)
{
    string line;
    while (getline(is, line))
        cout << line;
}

int main(int argc, char* argv[])
{
    if (argc == 1)
        Read(cin);
    else
    {
        ifstream in("sample.txt");
        Read(in);
    }
}

So, is it not complaining "no appropriate constructor available" ? Anyways, you can modify it as below.

void Read(istream& is)
{
    string line;
    while (getline(is, line))
        cout << line;
}

int main(int argc, char* argv[])
{
    if (argc == 1)
        Read(cin);
    else
    {
        ifstream in("sample.txt");
        Read(in);
    }
}
携君以终年 2024-08-30 23:27:11

你不能像这样影响流。不过,您可以使用指向 istream 的指针来获得您想要实现的目标。

#include <fstream>
#include <istream>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
  istream *in;
  // Must be declared here for scope reasons
  ifstream ifn;

  // No argument, use cin
  if (argc == 1) in = &cin;
  // Argument given, open the file and use it
  else {
    ifn.open(argv[1]);
    in = &ifn;
  }
  return 0;

  // You can now use 'in'
  // ...
}

You cannot affect streams like this. What you want to achieve can be obtained using a pointer to an istream though.

#include <fstream>
#include <istream>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
  istream *in;
  // Must be declared here for scope reasons
  ifstream ifn;

  // No argument, use cin
  if (argc == 1) in = &cin;
  // Argument given, open the file and use it
  else {
    ifn.open(argv[1]);
    in = &ifn;
  }
  return 0;

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