編集:コードに愚かなバグがあり、これを修正したので、今では説明できます。それに応じて投稿を更新しました。
次のコードは、ライン バッファー内でのシークが機能することを示しています。
まず、何かをプログラムにパイプしたときの結果。
$ 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
3 番目に、 /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;
}