How to keep the buffer of std::cin after using std::cin.getline()












0















I am trying to re-read the cin buffer after using cin.getline() 5 times, by calling cin.seekg(), but after calling cin.getline() again, instead of re-reading the data from the top, str becomes an empty string. Does cin.getline() flush the buffer? If so how do I stop that from happening?



#define PATH_MAX 512
using std::cin;

int main()
{
char* str = new char[PATH_MAX + 1];

for(int i = 0; i < 5; i++)
cin.getline(str, PATH_MAX);

cin.seekg(cin.beg);

while(true)
cin.getline(str, PATH_MAX);

return 0;
}









share|improve this question




















  • 1





    seekg ends up calling streambuf::seekpos: "The base class version of this function has no effect. The derived classes may override this function to allow absolute positioning of the position indicator." Classes like filebuf and stringbuf override it and do something useful, but cin isn't using one of those. Bottom line: you cannot seek on cin. If you want to look at the data a second time, read it and store it somewhere.

    – Igor Tandetnik
    Nov 25 '18 at 15:29








  • 1





    cin.seekg(cin.beg); is incorrect, it should just be cin.seekg(0);, but most likely is that std::cin simply isn't seekable. There's no requirement that it should be.

    – john
    Nov 25 '18 at 15:29


















0















I am trying to re-read the cin buffer after using cin.getline() 5 times, by calling cin.seekg(), but after calling cin.getline() again, instead of re-reading the data from the top, str becomes an empty string. Does cin.getline() flush the buffer? If so how do I stop that from happening?



#define PATH_MAX 512
using std::cin;

int main()
{
char* str = new char[PATH_MAX + 1];

for(int i = 0; i < 5; i++)
cin.getline(str, PATH_MAX);

cin.seekg(cin.beg);

while(true)
cin.getline(str, PATH_MAX);

return 0;
}









share|improve this question




















  • 1





    seekg ends up calling streambuf::seekpos: "The base class version of this function has no effect. The derived classes may override this function to allow absolute positioning of the position indicator." Classes like filebuf and stringbuf override it and do something useful, but cin isn't using one of those. Bottom line: you cannot seek on cin. If you want to look at the data a second time, read it and store it somewhere.

    – Igor Tandetnik
    Nov 25 '18 at 15:29








  • 1





    cin.seekg(cin.beg); is incorrect, it should just be cin.seekg(0);, but most likely is that std::cin simply isn't seekable. There's no requirement that it should be.

    – john
    Nov 25 '18 at 15:29
















0












0








0








I am trying to re-read the cin buffer after using cin.getline() 5 times, by calling cin.seekg(), but after calling cin.getline() again, instead of re-reading the data from the top, str becomes an empty string. Does cin.getline() flush the buffer? If so how do I stop that from happening?



#define PATH_MAX 512
using std::cin;

int main()
{
char* str = new char[PATH_MAX + 1];

for(int i = 0; i < 5; i++)
cin.getline(str, PATH_MAX);

cin.seekg(cin.beg);

while(true)
cin.getline(str, PATH_MAX);

return 0;
}









share|improve this question
















I am trying to re-read the cin buffer after using cin.getline() 5 times, by calling cin.seekg(), but after calling cin.getline() again, instead of re-reading the data from the top, str becomes an empty string. Does cin.getline() flush the buffer? If so how do I stop that from happening?



#define PATH_MAX 512
using std::cin;

int main()
{
char* str = new char[PATH_MAX + 1];

for(int i = 0; i < 5; i++)
cin.getline(str, PATH_MAX);

cin.seekg(cin.beg);

while(true)
cin.getline(str, PATH_MAX);

return 0;
}






c++ std cin istream






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 15:26







John Lannister

















asked Nov 25 '18 at 15:23









John LannisterJohn Lannister

325




325








  • 1





    seekg ends up calling streambuf::seekpos: "The base class version of this function has no effect. The derived classes may override this function to allow absolute positioning of the position indicator." Classes like filebuf and stringbuf override it and do something useful, but cin isn't using one of those. Bottom line: you cannot seek on cin. If you want to look at the data a second time, read it and store it somewhere.

    – Igor Tandetnik
    Nov 25 '18 at 15:29








  • 1





    cin.seekg(cin.beg); is incorrect, it should just be cin.seekg(0);, but most likely is that std::cin simply isn't seekable. There's no requirement that it should be.

    – john
    Nov 25 '18 at 15:29
















  • 1





    seekg ends up calling streambuf::seekpos: "The base class version of this function has no effect. The derived classes may override this function to allow absolute positioning of the position indicator." Classes like filebuf and stringbuf override it and do something useful, but cin isn't using one of those. Bottom line: you cannot seek on cin. If you want to look at the data a second time, read it and store it somewhere.

    – Igor Tandetnik
    Nov 25 '18 at 15:29








  • 1





    cin.seekg(cin.beg); is incorrect, it should just be cin.seekg(0);, but most likely is that std::cin simply isn't seekable. There's no requirement that it should be.

    – john
    Nov 25 '18 at 15:29










1




1





seekg ends up calling streambuf::seekpos: "The base class version of this function has no effect. The derived classes may override this function to allow absolute positioning of the position indicator." Classes like filebuf and stringbuf override it and do something useful, but cin isn't using one of those. Bottom line: you cannot seek on cin. If you want to look at the data a second time, read it and store it somewhere.

– Igor Tandetnik
Nov 25 '18 at 15:29







seekg ends up calling streambuf::seekpos: "The base class version of this function has no effect. The derived classes may override this function to allow absolute positioning of the position indicator." Classes like filebuf and stringbuf override it and do something useful, but cin isn't using one of those. Bottom line: you cannot seek on cin. If you want to look at the data a second time, read it and store it somewhere.

– Igor Tandetnik
Nov 25 '18 at 15:29






1




1





cin.seekg(cin.beg); is incorrect, it should just be cin.seekg(0);, but most likely is that std::cin simply isn't seekable. There's no requirement that it should be.

– john
Nov 25 '18 at 15:29







cin.seekg(cin.beg); is incorrect, it should just be cin.seekg(0);, but most likely is that std::cin simply isn't seekable. There's no requirement that it should be.

– john
Nov 25 '18 at 15:29














1 Answer
1






active

oldest

votes


















2















I am trying to re-read the cin buffer after using cin.getline() 5 times




That's not possible with cin, terminal based input respectively.



What you can do is keeping track of the read input yourself, using a std::vector<std::string> keeping those lines read in 1st place. Here's a rough sketch:



#include <iostream>
#include <string>
#include <vector>
using std::cin;
using std::string;

int main()
{
std::vector<string> lines;
string line;
for(int i = 0; i < 5; i++) {
std::getline(cin,line);
lines.push_back(line);
}

auto linepos = lines.begin();
while(linepos != lines.end()) {
// cin.getline(str, PATH_MAX); instead do:
// process *linepos
++linepos;
}
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53468959%2fhow-to-keep-the-buffer-of-stdcin-after-using-stdcin-getline%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2















    I am trying to re-read the cin buffer after using cin.getline() 5 times




    That's not possible with cin, terminal based input respectively.



    What you can do is keeping track of the read input yourself, using a std::vector<std::string> keeping those lines read in 1st place. Here's a rough sketch:



    #include <iostream>
    #include <string>
    #include <vector>
    using std::cin;
    using std::string;

    int main()
    {
    std::vector<string> lines;
    string line;
    for(int i = 0; i < 5; i++) {
    std::getline(cin,line);
    lines.push_back(line);
    }

    auto linepos = lines.begin();
    while(linepos != lines.end()) {
    // cin.getline(str, PATH_MAX); instead do:
    // process *linepos
    ++linepos;
    }
    }





    share|improve this answer




























      2















      I am trying to re-read the cin buffer after using cin.getline() 5 times




      That's not possible with cin, terminal based input respectively.



      What you can do is keeping track of the read input yourself, using a std::vector<std::string> keeping those lines read in 1st place. Here's a rough sketch:



      #include <iostream>
      #include <string>
      #include <vector>
      using std::cin;
      using std::string;

      int main()
      {
      std::vector<string> lines;
      string line;
      for(int i = 0; i < 5; i++) {
      std::getline(cin,line);
      lines.push_back(line);
      }

      auto linepos = lines.begin();
      while(linepos != lines.end()) {
      // cin.getline(str, PATH_MAX); instead do:
      // process *linepos
      ++linepos;
      }
      }





      share|improve this answer


























        2












        2








        2








        I am trying to re-read the cin buffer after using cin.getline() 5 times




        That's not possible with cin, terminal based input respectively.



        What you can do is keeping track of the read input yourself, using a std::vector<std::string> keeping those lines read in 1st place. Here's a rough sketch:



        #include <iostream>
        #include <string>
        #include <vector>
        using std::cin;
        using std::string;

        int main()
        {
        std::vector<string> lines;
        string line;
        for(int i = 0; i < 5; i++) {
        std::getline(cin,line);
        lines.push_back(line);
        }

        auto linepos = lines.begin();
        while(linepos != lines.end()) {
        // cin.getline(str, PATH_MAX); instead do:
        // process *linepos
        ++linepos;
        }
        }





        share|improve this answer














        I am trying to re-read the cin buffer after using cin.getline() 5 times




        That's not possible with cin, terminal based input respectively.



        What you can do is keeping track of the read input yourself, using a std::vector<std::string> keeping those lines read in 1st place. Here's a rough sketch:



        #include <iostream>
        #include <string>
        #include <vector>
        using std::cin;
        using std::string;

        int main()
        {
        std::vector<string> lines;
        string line;
        for(int i = 0; i < 5; i++) {
        std::getline(cin,line);
        lines.push_back(line);
        }

        auto linepos = lines.begin();
        while(linepos != lines.end()) {
        // cin.getline(str, PATH_MAX); instead do:
        // process *linepos
        ++linepos;
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 25 '18 at 15:51









        πάντα ῥεῖπάντα ῥεῖ

        73.6k1077144




        73.6k1077144
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53468959%2fhow-to-keep-the-buffer-of-stdcin-after-using-stdcin-getline%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            To store a contact into the json file from server.js file using a class in NodeJS

            Redirect URL with Chrome Remote Debugging Android Devices

            Dieringhausen