Using seekg() when taking input from redirected stdin

我与影子孤独终老i 提交于 2019-11-28 14:20:13

You can't seek on streams/pipes. They don't continue to exist in memory. Imagine the keyboard is directly connected to your program. The only operation you can do with a keyboard is ask for more input. It has no history.

If it's just a keyboard you can't seek, but if it's redirected with < in the shell seeking works fine:

#include <iostream>

int main() {
  std::cin.seekg(1, std::ios::beg);
  if (std::cin.fail()) 
    std::cout << "Failed to seek\n";
  std::cin.seekg(0, std::ios::beg);
  if (std::cin.fail()) 
    std::cout << "Failed to seek\n";

  if (!std::cin.fail()) 
    std::cout << "OK\n";
}

Gave:

user@host:/tmp > ./a.out
Failed to seek
Failed to seek
user@host:/tmp > ./a.out < test.cc
OK

You can't do that. std::cin is usually connected to a terminal, and so random access is out of the question.

You could do that if the stream you were using was a std::istringstream or an std::ifstream.

My advice is to read all the characters from std::cin into a single std::string, then create a std::istringstream from that string, and then try your techniques on that std::istringstream instead of std::cin.

You cannot seek on streams. You must unget the characters.

You can't seek on streams, but you can use either std::cin.peek() or std::cin.unget().

1) By using cin.peek()

char c;
while (c = cin.peek())
{
  //do stuff 
}

while (cin.get(c))
{
  //do stuff with the string a second time
}

2) By using cin.unget()

char c;
while (cin.get(c))
{
  //do stuff 
}

cin.unget();

while (cin.get(c))
{
  //do stuff with the string a second time
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!