检查stdin/std::cin是否支持查找

发布于 2024-10-24 08:41:38 字数 1998 浏览 6 评论 0原文

编辑:我在代码中遇到了一个愚蠢的错误并修复了这个问题,所以事情现在可以解释了。我相应地更新了我的帖子。

以下代码显示了在行缓冲区内查找的工作原理。

首先,将某些内容输入到程序中时的结果。

$ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

波浪屿的海角声 2024-10-31 08:41:38

在我看来,您的条件在这里颠倒了:

if (std::cin.fail()) {
    fprintf(stdout, "std::cin supports seekg().\n");
} else {
    fprintf(stdout, "std::cin does not support seekg().\n");
}

std::cin“支持seekg()”如果失败

Looks to me like you have your condition reversed here:

if (std::cin.fail()) {
    fprintf(stdout, "std::cin supports seekg().\n");
} else {
    fprintf(stdout, "std::cin does not support seekg().\n");
}

std::cin "supports seekg()" if it failed?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文