Thread: Using istream to read from memory?

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    1,619

    Using istream to read from memory?

    Is there an easy way to make an istream or child class that reads from a buffer in memory? I've mostly just used std::ifstream and read from files, but want to be able to read data from a memory buffer in the same way (e.g. I want to be able to use std::getline).

    I know I could make a child class (most likely of streambuf) and implement all the functions, but some part of me thinks there's a much easier way that I am just not seeing.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    You could use stringstreams. You don't have to open a file with a stringstream:
    Code:
    #include<iostream>
    #include<string>
    #include<sstream>
    using namespace std;
    
    
    int main()
    {
    	string str = "first line\nsecond line";
    	string input = "";
    	istringstream inFile(str);
    	
    	getline(inFile, input);
    	cout<<input<<endl;
    	
    	return 0;
    }

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    ...or if you are reading from a file, and you want to store all the lines of data, you can do this:
    Code:
    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <string>
    using namespace std;
    
    
    int main()
    {
    
    	ifstream inFile("C:\\TestData\\data.txt");
    	if(!inFile)
    	{
    		cout<<"couldn't open file"<<endl;
    		return 1;
    	}
    	
    	vector<string> myStrings; //You can use an array of strings here
    	string str = "";
    	
    	while(getline(inFile, str))
    	{
    		myStrings.push_back(str); //inserts the str in the back of the vector
    	}
    
    	cout<<myStrings[0]<<endl;
    
    	return 0;
    }
    Last edited by 7stud; 11-21-2005 at 07:28 PM.

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Thanks, that first way looks like what I need.

    It is a file that has been memory-mapped; I can't use ifstream because I need the filename to be in UTF-16 and sadly, C++ does not, at the moment anyway, allow wchar_t* filenames when opening a std::ifstream() or std::wifstream(). It's a proposed addition whenever the next C++ standard comes out but that doesn't really do me any good now .

    So I'm using WinAPI calls to memory-map the file. The stringstream method looks like it will work for subsequent reads on the file without too much pain and suffering.
    Last edited by Cat; 11-21-2005 at 11:18 PM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory leaks
    By TehOne in forum C Programming
    Replies: 4
    Last Post: 10-10-2008, 09:33 PM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 12:57 AM
  4. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 04:44 PM
  5. Can you read BIOS info from memory? 0xffff:0x0005
    By zahid in forum C Programming
    Replies: 5
    Last Post: 03-18-2003, 04:35 AM