fstream problem when reading file

Hey guys so I'm currently making a simple map editor using OpenGl and it is going really well until I got to the saving and loading part of my program.
I'm using fstream to save and load my data into a text file.

So originally when I first save my map, it looks like this:

10
10
0
NULL
NULL
A0001 A0001 A0001 A0001 A0001 A0001 A0011 A0001 A0001 A0001 
A0001 A0001 A0001 A0001 A0011 A0001 A0001 A0001 A0001 A0001 
A0001 A0001 A0011 A0011 A0001 A0011 A0011 A0001 A0001 A0001 
A0001 A0001 A0001 A0001 A0011 A0001 A0011 A0001 A0001 A0001 
A0001 A0011 A0011 A0001 A0001 A0001 A0001 A0011 A0001 A0001 
A0001 A0001 A0001 A0001 A0001 A0001 A0001 A0001 A0001 A0001 
A0001 A0001 A0011 A0011 A0001 A0001 A0011 A0011 A0001 A0001 
A0001 A0001 A0011 A0011 A0011 A0011 A0011 A0011 A0001 A0001 
A0001 A0001 A0011 A0001 A0001 A0001 A0011 A0011 A0001 A0001 
A0001 A0001 A0001 A0001 A0001 A0001 A0001 A0001 A0001 A0001 


But then when I load the map, it doesn't show my changes and when I saved again it becomes this....

10
10
0
NULL
NULL
A00010001 A0001 A0001 A0001 A0001 A0001 A0001 A0001 A0001 A0001 
A0001 A0001 A0001 A0001 A0001 A0001 A0001 A0001 A0001 A0001 
A0001 A0001 A0001 A0001 A0001 A0001 A0001 A0001 A0001 A0001 
A0001 A0001 A0001 A0001 A0001 A0001 A0001 A0001 A0001 A0001 
A0001 A0001 A0001 A0001 A0001 A0001 A0001 A0001 A0001 A0001 
A0001 A0001 A0001 A0001 A0001 A0001 A0001 A0001 A0001 A0001 
A0001 A0001 A0001 A0001 A0001 A0001 A0001 A0001 A0001 A0001 
A0001 A0001 A0001 A0001 A0001 A0001 A0001 A0001 A0001 A0001 
A0001 A0001 A0001 A0001 A0001 A0001 A0001 A0001 A0001 A0001 
A0001 A0001 A0001 A0001 A0001 A0001 A0001 A0001 A0001 A0001 


In my map.cpp, I have this....
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
void Map::setUpMap()
{
	int id = 0; //tile ID so that we can 

	world = new TileLocation ** [mapSize_y];
	
	for (int rows = 0; rows < mapSize_y; rows++)
		world [rows] = new TileLocation * [mapSize_x];

	for (int rows = 0;rows<mapSize_y; rows++) //defines every array info
	{
		for (int col=0;col<mapSize_x; col++)
		{
				world[rows][col]=NULL;
		}
	}

	for (int rows = 0; rows < mapSize_y; rows++) // Array for the game grid, assigns attributes of the tiles 
	{
		for (int col = 0;col < mapSize_x; col++)
		{
				world[rows][col]=new TileLocation(id, rows, col); //gives each tile an id
				id++;
		}
	}
}

void Map::editTile(int x, int y)
{
	world[y/32][x/32]->setTile(1);
	cout << x/32 << " " << y/32 <<endl; 
}

void Map::loadMap(string fileName)
{
	ifstream fin(fileName+".dat");
	
	fin >> mapSize_x >> mapSize_y >> weather >> background >> BK_MUSIC;

	setUpMap();

	for (int rows = 0; rows < mapSize_y; rows++) // Array for the game grid, assigns attributes of the tiles 
	{
		for (int col = 0;col < mapSize_x; col++)
		{
				world[rows][col] -> read(fin); //gives each tile an id
		}
	}

	fin.close();
}

void Map::saveMap(string fileName)
{
	ofstream fout(fileName+".dat");

	fout << mapSize_x << endl << mapSize_y << endl << weather << endl << background << endl << BK_MUSIC << endl;

	for (int row = 0; row < mapSize_y; row++)
	{
		for (int col = 0; col < mapSize_x; col++)
			world [row][col] -> write(fout);
		fout<<endl;
	}

	fout.flush();
	fout.close();
}


and in my Tile.cpp, I have this...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
TileLocation::TileLocation(int i, int row, int col)
{
	type = TILE; 
	id = i;
	file = "A";
	solid = true;
	layer = 0;
	tile = 0;
	x = col * TILE_WIDTH;
	y = row * TILE_HEIGHT;
}

void TileLocation::read(ifstream &fin)
{
	string junk;

	fin >> file >> type >> layer >> tile >> solid >> junk;
}

void TileLocation::write(ofstream &fout)
{
	fout << file << type << layer << tile << solid << " ";
}


I have a feeling the problem lies in the read function in the tile.cpp
But I believe it's also the write function.....

Thank you
fin >> file >> type >> layer >> tile >> solid >> junk;
First it will read A0001 to file (it is string, right?) the n it will try to read next A0001 into enum type, fails, sets stream into failed state and nothing would be read after that. Even if you change file variable from string to char, any int reading will read all numbers until whitespace.
You need custom parsing function to successully read your packed record.
Solved it!
Thanks for describing how ifstream works....
So I changed it so fin just reads junk string. Afterwards, I get each individual character of the string and assign them to the relative variables.
like so...
1
2
3
4
5
6
7
8
9
10
11
12
void TileLocation::read(ifstream &fin)
{
	string junk;

	fin >> junk;

	file = junk[0];
	type = junk[1] - '0';
	layer = junk[2] - '0';
	tile  = junk[3] - '0';
	solid = junk[4] - '0';
}
Topic archived. No new replies allowed.