C++ Fstream I/O

Started by
5 comments, last by sinyester 14 years, 6 months ago
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!
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 inputstd::stringstream ss( numberString ); // Inserts numberString into a new stringstreamif ( !(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]
I get... (when loading number 2)

0013FF24 values when i use that code :o
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)
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

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] = {};
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 :)

This topic is closed to new replies.

Advertisement