Using seekg() when taking input from redirected stdin

后端 未结 4 2027
情话喂你
情话喂你 2020-12-12 04:48

So i\'m trying to read in a string of characters twice using cin.get(). The input is being redirected as \"program < input\". So it is valid to use seekg().

As th

相关标签:
4条回答
  • 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.

    0 讨论(0)
  • 2020-12-12 05:29

    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

    0 讨论(0)
  • 2020-12-12 05:29

    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
    }
    
    0 讨论(0)
  • 2020-12-12 05:39

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

    0 讨论(0)
提交回复
热议问题