ifstream woes

Started by
3 comments, last by Fruny 15 years, 1 month ago
Im having trouble using getline(fin, temp.name); Heres the code: ////This is the maps.txt file contents
Number of Tilesets=2
Name=johnny
Width=32
Height=64
TileCount=30
Bitmap=Johnny.bmp
Name=Poops
Width=11
Height=34
TileCount=12
Bitmap=poopydoopy.bmp
//This is the source code
#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

void main()
{

	struct TileSet
	{
		string name;
		int width;
		int height;
		int tcount;
		string gfx;
	};

	//TileSet green;
	vector <TileSet> tilesets;

	ifstream in;

	in.open("maps.txt");

	if (in.fail() || !in.is_open())
	{
		cerr << "no data/File does not exist.";
		exit(0);
	}

	int nts = 0, nmaps = 0;
	int i = 0;

	while (1)
	{
		if (in.get() == '=')
		{
			in >> nts;
			cout << "nts: " << nts << endl;
			break;
		}
	}

	while (!in.eof())
	{
		TileSet temp;
		if (in.get() == '=')
			//cout << in.get();
		{
			if (nts > 0)
			{
				++i;
				switch(i)
				{
				case 1:
					getline(in, temp.name);
					break;
				case 2:
					in >> temp.width;
					break;
				case 3:
					in >> temp.height;
					break;
				case 4:
					in >> temp.tcount;
					break;
				case 5:
					getline(in, temp.gfx);
					--nts;
					i = 0;
					tilesets.push_back(temp);
					break;
				default:
					cout << "Unknown\n";
				}
			}
		}


	}

	for (int i = 0; i < tilesets.size(); ++i)
	{
		cout << "Name: " << tilesets.name << "\nWidth: " << tilesets.width << "\nHeight: " << tilesets.height;
		cout << "\nTCount: " << tilesets.tcount << "\nBitmap: " << tilesets.gfx << endl;
	}

	//cout << (char)87;

	in.close();

}
it all seems to work except
	case 1:
		getline(in, temp.name);
is capturing nothing. i had it working before but i cant seem to find out why its no longer working, especially when case 5 is. anyone know whats going on? Edit by Fruny - Added code tags
Advertisement
Printing out temp.name just after reading it shows it's been read properly, so the error isn't with ifstream or getline, you're doing that much correctly.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Take 'temp' out of the while loop, so that it is not destroyed and reconstructed every time you read a line. For the basic-types member variables, you're just being "lucky": they do not have a constructor and the function is recreating the structure in the same memory location (it's a local variable), so the values are preserved. However, the string members have a constructor and so are reset to "" when TileSet structure is recreated. This is why you only get the last string member, which was set on the same iteration where you put it in the vector.

	while (!in.eof())	{		TileSet temp;


should be

	TileSet temp;	while (!in.eof())	{


You might clear temp's contents right after storing it in the vector, or directly add a new element to the vector when reading the name and then directly setting its members (accessing it with tilesets.back()) when reading the other members.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
ok i can see that this is the problem. after realizing it wasnt getline/ifstream i thought if there are not string asignment constructors or something of that nature but obviously that isnt the issue.

im just puzzled that if the variable is created in a loop, but still assigned data members before being passed to the vector why it isnt properly constructing
Quote:Original post by kickstep
im just puzzled that if the variable is created in a loop, but still assigned data members before being passed to the vector why it isnt properly constructing


I'm afraid I don't really understand what you mean here.

You are not passing the variable to the vector during the same iteration when the data members are assigned. In the meantime, the variable has been destroyed, then recreated. It is not the same variable anymore. You cannot expect the members to still be the same.

* create temp
* read temp.name
* destroy temp
* create temp <-- here temp.name has been reset
* read temp.width
* destroy temp
* create temp
* read temp.height
* destroy temp
* create temp
* read temp.tcount
* destroy temp
* create temp
* read temp.gfx
* store temp in vector
* destroy temp
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement