【发布时间】:2015-03-04 11:08:46
【问题描述】:
我刚刚遇到以下行为,想知道背后的原因:
假设一个这样的简化程序
...
{
std::ifstream in(argv[1]);
assert(in.good());
while (std::getline(in, line)) {
// Area 1
}
in.close();
}
{
std::ifstream in(argv[1]);
assert(in.good());
while (std::getline(in, line)) {
// Area 2
}
in.close();
}
如果这样的程序是这样调用的:
./myProg xxx
两个,区域 1 和区域 2 将被输入 n 次,其中 n 是 xxx 中的行数。
但是,如果我这样调用(使用 bash):
./myProg <(head -n 100 xxx)
区域 1 将被输入 100 次,区域 2 将被输入 0 次。两个断言(in.good())都通过了。显然,第二个传递了一个文件描述符(如果我打印参数,则类似于 /dev/fd/63)而不是一个实际的文件,并且这个东西可以打开以读取一次 - 但是当打开两次时,它似乎是空的第二次调用。
不知道是什么原因。
【问题讨论】:
标签: c++ linux file ifstream file-descriptor