Jump to content
  • Advertisement
Sign in to follow this  
sinyester

C++ Fstream I/O

This topic is 3301 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! I'm loading my tilemap(format below) for my game from textfiles. But I realize that I need more than 0-9 textures obviously.. 2 2 2 2 2 2 2 0 0 0 0 0 5 5 0 4 0 2 0 0 0 9 4 9 0 2 0 0 0 4 0 0 2 2 2 0 0 0 2 0 0 0 0 0 2 8 8 0 2 So my question is; how do I change so it can load this format? (doesn't matter if there's a comma or some other symbol to seperate) 22 3 2 2 2 2 21 14 0 0 0 0 5 52 0 44 0 2 0 20 0 9 48 69 0 2 0 0 0 43 0 0 2 2 52 0 0 20 2 0 20 0 0 0 2 8 8 24 42
// LOAD FILEMAP TO map[][]
if((GetKeyState('I') & 0x80))
{	
	ifstream load("txt/tilemap.txt");
	for (int y = 0; y < mapy; y++)
	{
		for (int x = 0; x < mapx; x++)
		{
			map[y][x] = load.get() - '0';
		}
		load.get();
	}
	load.close();
}
Thanks in advance!

Share this post


Link to post
Share on other sites
Advertisement
load.get() extracts a single char. The extraction operator (operator >>) extracts formatted data from a stream.


if ( ( GetKeyState('I') & 0x80 ) )
{
ifstream file( "txt/tilemap.txt" );
for ( y = 0; y < mapy; ++y )
for ( x = 0; x < mapx; ++x )
file >> map[y][x];
} // No need to call file.close(); the RAII idiom takes care of it when "file" falls out of scope



Note that invalid data will cause file to enter a failure state and will refuse to extract any more data until the error state is cleared and the offending input is removed/skipped. A much safer alternative is to use boost::lexical_cast or to use a separate stream:


int myNumber = -1;
std::string numberString;
file >> numberString; // Extracts a whitespace separated token, may or may not be offending input
std::stringstream ss( numberString ); // Inserts numberString into a new stringstream
if ( !(ss >> myNumber ) ) // Attempt to extract a number from the stringstream
; // Failure in extracting the number; the stringstream
// is set at a failure state, but we don't care :)
// the offending input from the file has already been extracted
// and we can move on ...



[Edited by - _fastcall on October 30, 2009 1:40:56 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by sinyester
I get... (when loading number 2)

0013FF24 values when i use that code :o


What type is map?
Did the original puzzle load correctly?
What do you mean by 1310500 values when you which code? (I modified my post a few times)

Share this post


Link to post
Share on other sites
My map is char;

char map[48][48] = {
{0, 0, 0, 0, 0, and so on....

Yes the code i wrote in my first post works for 0-9

No idea what you mean there :o

Share this post


Link to post
Share on other sites
If map is of char type, then file >> map[y][x] will only extract a single character. You'll need to extract a temporary integer from file then cast it to a char, or change map to an integer type:


int temp;
file >> temp;
map[y][x] = static_cast<char>( temp ); // Note, range of "char" is from -127 to 128

// Or:

int map[48][48] = {};

Share this post


Link to post
Share on other sites
That works. Sorry for not mention that map was of char type ( i'm pretty new to this )

So.. If I would want to change my char array to an int array I have to do.. ?
: edit, ignore that, I just changed it to:
int map[48][48] = {
{0, 0, 0,


Thanks for all of your help :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!