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!!! :-)
From file to memory
Hi there!
Until now I''ve been reading my files with a simple stream. Like this:
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
Game Development Wiki
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.
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.
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:
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement