Sign in to follow this  

ifstream woes

This topic is 3194 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

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[i].name << "\nWidth: " << tilesets[i].width << "\nHeight: " << tilesets[i].height;
		cout << "\nTCount: " << tilesets[i].tcount << "\nBitmap: " << tilesets[i].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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

This topic is 3194 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this