有条件地从std输入或文件中读取文件
Conditionally reading file from std input or a file
这里是新的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;
注意input
和output
声明中的&
。它们表明我们并没有声明一个单独的流对象,而是声明一个对其他流对象的引用,我们根据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
继承istream
,ofstream
继承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;
}
这不是唯一的解决方案;可能有更干净的
问题是,你必须在你的条件中声明块外的流,这样它就不会超出范围,因为你想在外面使用它。
相关文章:
- 正在将csv文件读取为双精度矢量
- 将从格式化文本文件读取的文本数据存储到链表
- C++ 读取文件读取文件不正确
- 使用对象文件读取三角形数据网格
- 如何减慢从 BVH 文件读取的 opengl 动画?
- 从文件读取,并写入函数C++中的数组
- 从对象C++中的文件读取内容时出现分段错误
- 从文件读取并通过矢量输出到控制台
- 尝试将字符串从文件读取到无符号字符向量中
- 如何将文本文件读取到二维数组中并以 c++ 打印
- 简化文件读取的函数
- 如何将逗号分隔的文件读取为 2D int 数组?
- 在 c++ 中将数据从文件读取到队列中
- 'std::wstring_convert'尽可能多地转换(从 UTF8 文件读取块)
- C++,从文件读取到结构,然后读取到向量(结构被推入向量太多次,而不仅仅是一次)
- 文件读取C++包括空格键
- 从文件读取时未将值推回矢量
- 从文件 C++ 读取时的无限循环
- 从同一文件读取和写入?
- 从.txt文件读取到C++中的双精度向量