有条件地从std输入或文件中读取文件

Conditionally reading file from std input or a file

本文关键字:文件 读取 输入 std 有条件      更新时间:2023-10-16

这里是新的C++程序员。我正在尝试编写一个命令行应用程序,它接受两个参数,一个输入文件和一个输出文件。但是,如果输入文件或输出文件名是"-",我需要程序读取/输出到标准输入/输出。我的问题是,在C++中,如果编译器不知道输入/输出流已初始化,我不知道如何做到这一点。这是我的密码。

if(argv[1] == "-") {
  istream input;
  cout << "Using standard input" << endl;
}
else {
  ifstream input;
  cout << "Using input file " << argv[1] << endl;
  input.open(argv[1], ios::in);
  if(!input) {
    cerr << "Cannot open input file '" << argv[1]
    << "', it either does not exist or is not readable." << endl;
    return 0;
  }
}
if(argv[2] == "-") {
  ostream output;
  cout << "Using standard output" << endl;
}
else {
  ofstream output;
  cout << "Using output file " << argv[2] << endl;
  output.open(argv[2], ios::out);
  if(!output) {
    cerr << "Cannot open output file '" << argv[2] << "' for writing."
    << " Is it read only?" << endl;
    return 0;
  }
}

从这里开始,我不能在输入时调用运算符>>,因为我猜编译器不知道它已经初始化。

您可以使用流的引用,然后将其初始化为引用文件流或标准输入或输出。初始化必须在一个命令中进行,因此即使不使用文件流,也必须声明文件流。

ifstream file_input;
istream& input = (strcmp(argv[1], "-") == 0) ? cin : file_input;
ofstream file_output;
ostream& output = (strcmp(argv[2], "-") == 0) ? cout : file_output;

注意inputoutput声明中的&。它们表明我们并没有声明一个单独的流对象,而是声明一个对其他流对象的引用,我们根据argv[x]的值有条件地选择它。

如果需要的话,你可以打开文件。缺点是我们需要检查两次"-"字符串,而不是每次输入或输出只检查一次。

if (strcmp(argv[1], "-") == 0) {
  cout << "Using standard input" << endl;
} else {
  cout << "Using input file " << argv[1] << endl;
  file_input.open(argv[1]);
  if (!file_input) {
    cerr << "Cannot open input file '" << argv[1]
         << "', it either does not exist or is not readable." << endl;
    return 1;
  }
}

之后,您可以从input读取并写入output,并且将使用文件或标准I/O流。


请注意我对您的代码所做的其他更改。首先,我调用strcmp,而不是使用==运算符;当将CCD_ 9与文字进行比较时,运算符不会执行您认为它会执行的操作。接下来,当打开文件失败时,我返回1而不是0。零表示程序成功,而非零表示OS程序失败。

您可以在条件语句之外声明成员,因为ifstream继承istreamofstream继承ostream。为了避免切片,请使用指针:

istream* input = NULL;
bool isFile = false;
if(argv[1] == "-") {
  input = new istream;
}
else {
  input = new ifstream;
  isfile = true;
}

然后,无论您想在哪里使用input,都只需将其转换为正确的类型:

if (isFile)
{
    ifstream* finput = (ifstream*)input;
}

这不是唯一的解决方案;可能有更干净的

问题是,你必须在你的条件中声明块外的流,这样它就不会超出范围,因为你想在外面使用它。