I am tring to convert std:string to char* in several way, however non of these work:

first way:

char* c_levelMapFile = new char[levelMapFile.length() + 1]; 
	strcpy(c_levelMapFile, levelMapFile.c_str());

	ifstream myfile(c_levelMapFile);

myfile.is_open(); // returns false

second way:

const char* c_levelMapFile = levelMapFile.append("\0");

	ifstream myfile(c_levelMapFile);

myfile.is_open(); // returns false

So in both cases it fails. If I specify the input file manually, then it's working. So I guess the problem should be in conversion from std::string to char* and probably for some reasons it is not adding the terminating zero at the end of the c-stryle string.

That was my immediate thought as well, but for some reasons it's not working. I though that could be something to do with the terminating zero, so I have tried to do it other way, however all failed :(

Here is the method code, as the whole class code is 350 lines.

void ThreeDCubeGame::InitialiseLevel()
{
	using namespace std;
	// Destroy any moving objects for the current level - releasing the memory
	DestroyOldObjects();


	char s[MAP_WIDTH];
	bool defaultMap = false;
	string levelMapFile;
	

	switch(m_iLevelNumber)
	{
	case 0: levelMapFile = "level0.map"; break;
	case 1: levelMapFile = "level1.map"; break;
	case 2: levelMapFile = "level2.map"; break;
	case 3: levelMapFile = "level3.map"; break;

	default: defaultMap = true;
	}

	
	char* c_levelMapFile = new char[levelMapFile.length() + 1]; // not freeing up memory as it will dissappear with the stack frame
	strcpy(c_levelMapFile, levelMapFile.c_str());

	ifstream myfile(c_levelMapFile);// !!! THIS DOESN'T WORK !!!

        ifstream myfile(levelMapFile.c_str());// !!! THIS DOESN'T WORK !!!


	//ifstream myfile("level0.map");// !!! THIS WORKS !!!

        // ---- Game Map is generated here -----
	if (myfile.is_open() && !defaultMap)
	{
		for ( int y = 0 ; y < MAP_HEIGHT ; y++ )
		{
			myfile >> s;
			
			for ( int x = 0 ; x < MAP_WIDTH ; x++ )
			{
				m_acMapData[x][y] = s[x];
				if(m_acMapData[x][y] != '-' )
				{
					AddBlocks(m_acMapData[x][y] - 'A');
					cout << m_acMapData[x][y] - 'A';
				}
			}		
		}
		myfile.close();
	}// loading the map from memory
	else
	{
		cout << "Unable to open file, loading default map!\n";

		for ( int y = 0 ; y < MAP_HEIGHT ; y+=2 )
			for ( int x = 0 ; x < MAP_WIDTH ; x+=2 )
			{
				m_acMapData[x][y] = '-';
				m_acMapData[x+1][y] = 'A' + rand()%1;
				m_acMapData[x][y+1] = 'A' + rand()%2;// !!! original values was "rand()%8" !!!

				AddBlocks(m_acMapData[x][y+1] - 'A');
				AddBlocks(m_acMapData[x+1][y] - 'A');
			
				m_acMapData[x+1][y+1] = '-';
			}
	}

	// Count the number of things which need to be collected.
	m_iTileNumber = 0;
	for ( int y = 0 ; y < MAP_HEIGHT ; y++ )
		for ( int x = 0 ; x < MAP_WIDTH ; x++ )
		{
			
			switch( m_acMapData[x][y] )
			{
				case 'A': break;
				case '-': break;
				
				default: m_iTileNumber++; break;
			}
		}

	// Create the moving objects for the new level
	InitialiseObjects();
}

Edited 6 Years Ago by kudresov: n/a

>>> ifstream myfile(levelMapFile.c_str());// !!! THIS DOESN'T WORK !!!

This should work fine. Are you sure that "levelMapFile" has a value? Because if "m_iLevelNumber" is < 0 or > 3 "levelMapFile"will have no value at all.

Yes, it does has value, here is the screenshot from the watch window, during the debugging :

Attachments Screen_shot_2009-12-06_at_20.10_.02_.png 26 KB
This article has been dead for over six months. Start a new discussion instead.