Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

VanKurt

From file to memory

This topic is 5305 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there! Until now I''ve been reading my files with a simple stream. Like this:
ifstream fin("FunnyFile.txt");
	string str1 = "";
	while( fin >> str1 )
	{
		if( str1 == "ADD_MONSTER" )
		{
			// Create monster

		}// end if


		if( str1 == "ADD_GOLD" )
		{
			// Add more gold

		}// end if

	}// end while

	fin.close();
That is really simple and works well. But now I have changed my file structure: All files are now packed in a single .dat file. To work with the files I have a hand full of functions. One to load a desired file into memory, one to get a pointer to its data and one to get the file''s length. What I''d like to know now is how I have toc change my reading-code, so that it works with my new pack file. 1) How do I initialize my stream? What should be put into the constructor (instead of "FunnyFile.txt") ??? 2) Do I have to change the reading code inside the while? 3) How do I now know when to stop? When working with a file the while exits when the EOF is reached. But when reading from memory the loop will continue endlessly, right? Thanks for your support!!! :-)

Share this post


Link to post
Share on other sites
Advertisement
You''ll probably have to build a custom stream class to work with your special file format. Overload the << and >> operators and maybe a couple other things, and the rest of your code should work unchanged.

Game Development Wiki

Share this post


Link to post
Share on other sites
I don''t think so, but I''m not really sure. I prefer C style I/O myself. But there might be some other stream for reading and writing directly to memory.

This might be helpful.

BTW, do you have a single file with multiple files inside it (like a .zip or .pak) that you want to read in, then you''re loading the entire archive file into memory, then exctracting the desired file out of memory? If so, that probably isn''t the best way, especially once the archive begins to grow to several MB.

Share this post


Link to post
Share on other sites
If you have the pointer and the length, you can use a pointer range to initialize a std::stringstream object with the contents of the dat file. ex:

void buildstream(const char * start, int length) {
std::string str(start, start + length);
std::stringstream sstr(str);
// use the stringstream
}


It''s less efficient that using strstreams (which will operate on a user supplied buffer), but suggesting strstreams could get me crucified by a roving language purist.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!