使用 stdio 重定向 Unix 和C++上的输入和输出文件
Redirecting input and output files on Unix and C++ using stdio
我需要这样做:
$ ./compiledprog.x < inputValues > outputFile
这样我就可以从文件中读取inputValues
对于我们的情况,这些文件可能只是n
int
值或其他值分开。 然后printf()
任何事情都会进入outputFile
. 但是从技术上讲,这叫什么,我在哪里可以找到这样做的演示。
正如其他人所指出的,它是输入/输出重定向。
下面是一个示例程序,它将标准输入复制到标准输出,在您的示例中,将内容从 inputValues 复制到 outputFile。在程序中实现您想要的任何逻辑。
#include <unistd.h>
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::cerr;
#include <string>
using std::string;
int main(int argc, char** argv) {
string str;
// If cin is a terminal, print program usage
if (isatty(fileno(stdin))) {
cerr << "Usage: " << argv[0] << " < inputValues > outputFile" << endl;
return 1;
}
while( getline(cin, str) ) // As noted by Seth Carnegie, could also use cin >> str;
cout << str << endl;
return 0;
}
注意:这是快速而脏的代码,需要行为良好的文件作为输入。可以添加更详细的错误检查。
这
称为 I/O 重定向。
相关文章:
- 字符的输入/输出操作
- C++ 中文件输入输出中的长长 v/s int
- 要求用户输入C++输出文件名
- 在C++中重载输入/输出运算符
- 从用户获取输入/输出
- 如何选择waveIn/waveOut函数C++使用的输入/输出设备
- C++击球平均值程序使用输入/输出文件打印名称,平均值,最高/最低平均值时遇到麻烦
- 从 2 位输入输出 4 位数字年份
- 标准输入输出操作如何C++完成
- Windows 中应用程序的每秒输入/输出操作数
- 如何初始化可以设置为默认数组并将输入/输出作为数组的类函数?
- 输入/输出和类
- 使用 Octave GNU 访问输入/输出文件
- (C++)分解这个简单的输入/输出代码:
- TensorFlow模型:如何从原始爱好文件中识别输入/输出节点名称
- 干净取消在输入/输出调用中被阻止的 std::线程
- 将线程之间的标准输入/输出重定向
- 输入/输出异或字符串的 C++ 问题
- C++输入/输出错误
- C++输入输出