How can I read from memory just like from a file using iostream?

前端 未结 6 1027
醉话见心
醉话见心 2020-12-09 12:41

I have simple text file loaded into memory. I want to read from memory just like I would read from a disc like here:

ifstream file;
string line;

file.open(\         


        
相关标签:
6条回答
  • 2020-12-09 13:19

    Use

    std::stringstream
    

    It has an interface to manipulate and read strings just like other streams.

    0 讨论(0)
  • 2020-12-09 13:19

    Here's how I would do it:

    #include <sstream>
    
    std::istringstream stream("some textual value");
    std::string line;
    while (std::getline(stream, line)) {
        // do something with line
    }
    

    Hope this helps!

    0 讨论(0)
  • 2020-12-09 13:23

    Use boost.Iostreams. Specifically basic_array.

    namespace io = boost::iostreams;
    
    io::filtering_istream in;
    in.push(array_source(array, arraySize));
    // use in
    
    0 讨论(0)
  • 2020-12-09 13:28

    You can use istringstream for that.

    string text = "text...";
    istringstream file(text);
    string line;
    
    while(file.good())
    {
        getline(file,line);         
    }
    
    0 讨论(0)
  • 2020-12-09 13:29

    I found a solution that works on VC++ since Nim solution works only on GCC compiler (big thanks, though. Thanks to your answer I found other answers which helped me!).

    It seems that other people have similar problem too. I did exactly as here and here.

    So to read from a piece of memory just like form a istream you have to do this:

    class membuf : public streambuf
    {
        public:
            membuf(char* p, size_t n) {
            setg(p, p, p + n);
        }
    };
    
    int main()
    {
        char buffer[] = "Hello World!\nThis is next line\nThe last line";  
        membuf mb(buffer, sizeof(buffer));
    
        istream istr(&mb);
        string line;
        while(getline(istr, line))
        {
            cout << "line:[" << line << "]" << endl;
        }
    }
    

    EDIT: And if you have '\r\n' new lines do as Nim wrote:

    if (*line.rbegin() == '\r') line.erase(line.end() - 1);
    

    I'm trying to treat this memory as as wistream. Does anybody know how to do this? I asked separate question for this.

    0 讨论(0)
  • 2020-12-09 13:36

    You can do something like the following..

    std::istringstream str;
    str.rdbuf()->pubsetbuf(<buffer>,<size of buffer>);
    

    And then use it in your getline calls...

    NOTE: getline does not understand dos/unix difference, so the \r is included in the text, which is why I chomp it!

      char buffer[] = "Hello World!\r\nThis is next line\r\nThe last line";  
      istringstream str;
      str.rdbuf()->pubsetbuf(buffer, sizeof(buffer));
      string line;
      while(getline(str, line))
      {
        // chomp the \r as getline understands \n
        if (*line.rbegin() == '\r') line.erase(line.end() - 1);
        cout << "line:[" << line << "]" << endl;
      }
    
    0 讨论(0)
提交回复
热议问题