检查stdin/std::cin是否支持查找
编辑:我在代码中遇到了一个愚蠢的错误并修复了这个问题,所以事情现在可以解释了。我相应地更新了我的帖子。
以下代码显示了在行缓冲区内查找的工作原理。
首先,将某些内容输入到程序中时的结果。
$ echo 'asdf' | ./seektest
stdin does not support fseek()
c == s
std::cin does not support seekg()
Second read to std::cin failed!
c == ?
Second read to std::cin failed!
c == ?
其次,我输入“a[enter]s[enter]d[enter]f[enter]”。
$ ./seektest
a
stdin supports fseek().
s
c == s
std::cin supports seekg().
d
c == d
c ==
第三,我为每个 getc
/get()
调用键入“asdf”。
$ ./seektest
asdf
stdin supports fseek().
asdf
c == a
std::cin supports seekg().
asdf
c == a
c == s
查找似乎在行缓冲区内进行。
这是代码。
#include <iostream>
#include <cstdio>
int main(int argc, char ** argv)
{
// Try cstdio.
int x = fgetc(stdin);
if (x < 0) {
fprintf(stderr, "First read to stdin failed!.");
}
int res = fseek(stdin, -1, SEEK_CUR);
if (!res) {
fprintf(stdout, "stdin supports fseek().\n");
} else {
fprintf(stdout, "stdin does not support fseek()\n");
}
x = fgetc(stdin);
if (x < 0) {
fprintf(stderr, "Second read to stdin failed!\n");
}
char c = x;
fprintf(stdout, "c == %c\n", c);
// Try iostream.
x = std::cin.get();
if (std::cin.fail()) {
fprintf(stderr, "First read to std::cin failed!\n");
}
std::cin.seekg(-1, std::ios::cur);
if (std::cin.fail()) {
fprintf(stdout, "std::cin does not support seekg()\n");
} else {
fprintf(stdout, "std::cin supports seekg().\n");
}
c = std::cin.get();
if (std::cin.fail()) {
fprintf(stderr, "Second read to std::cin failed!\n");
}
fprintf(stdout, "c == %c\n", c);
c = std::cin.get();
if (std::cin.fail()) {
fprintf(stderr, "Second read to std::cin failed!\n");
}
fprintf(stdout, "c == %c\n", c);
return 0;
}
EDIT: I had a stupid bug in the code and fixed this, so things are explicable now. I updated my post accordingly.
The following code shows that seeking within the line buffer works.
First, the result when piping something into the program.
$ echo 'asdf' | ./seektest
stdin does not support fseek()
c == s
std::cin does not support seekg()
Second read to std::cin failed!
c == ?
Second read to std::cin failed!
c == ?
Second, I typed "a[enter]s[enter]d[enter]f[enter]".
$ ./seektest
a
stdin supports fseek().
s
c == s
std::cin supports seekg().
d
c == d
c ==
Third, I typed "asdf" for each getc
/get()
call.
$ ./seektest
asdf
stdin supports fseek().
asdf
c == a
std::cin supports seekg().
asdf
c == a
c == s
Seeking appears to work within the line buffer.
Here is the code.
#include <iostream>
#include <cstdio>
int main(int argc, char ** argv)
{
// Try cstdio.
int x = fgetc(stdin);
if (x < 0) {
fprintf(stderr, "First read to stdin failed!.");
}
int res = fseek(stdin, -1, SEEK_CUR);
if (!res) {
fprintf(stdout, "stdin supports fseek().\n");
} else {
fprintf(stdout, "stdin does not support fseek()\n");
}
x = fgetc(stdin);
if (x < 0) {
fprintf(stderr, "Second read to stdin failed!\n");
}
char c = x;
fprintf(stdout, "c == %c\n", c);
// Try iostream.
x = std::cin.get();
if (std::cin.fail()) {
fprintf(stderr, "First read to std::cin failed!\n");
}
std::cin.seekg(-1, std::ios::cur);
if (std::cin.fail()) {
fprintf(stdout, "std::cin does not support seekg()\n");
} else {
fprintf(stdout, "std::cin supports seekg().\n");
}
c = std::cin.get();
if (std::cin.fail()) {
fprintf(stderr, "Second read to std::cin failed!\n");
}
fprintf(stdout, "c == %c\n", c);
c = std::cin.get();
if (std::cin.fail()) {
fprintf(stderr, "Second read to std::cin failed!\n");
}
fprintf(stdout, "c == %c\n", c);
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我看来,您的条件在这里颠倒了:
std::cin
“支持seekg()
”如果失败?Looks to me like you have your condition reversed here:
std::cin
"supportsseekg()
" if it failed?