Archived

This topic is now archived and is closed to further replies.

More file reading problems

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

Ok, I have an array of ints called s_Type[10] And I have the following function that reads data in from a text file:
int CLevel::InitLevel(char *dir)
{
	char filename[255];		// The filename

	char buffer[255];			// Buffer	

	FILE * fp;						// The file


  sprintf(filename, "%s/leveldata", dir);		// Load the filename into a string

	
	// Try to open the file

  if((fp=fopen(filename, "rb")) == NULL)
  {
		// If the file could not be opened, print an error message

    printf("ERROR opening file %s\n\n", filename);
    return 1;
  }

	// Get the first line of the file to set the current level

	fgets(buffer, 255, fp);
	sscanf(buffer, "LEVEL: %d", &m_LevelData.s_Level);
	while(!feof(fp))
  {
    fgets(buffer, 255, fp);
    if(buffer[0] != '#' && buffer[0] != '\r' && buffer[0] != '\0' && buffer[0] != '\n' && strlen(buffer) != 0)
    {
			sscanf(buffer, "NEXT LEVEL: %s",	&m_LevelData.s_NextLevel);
			sscanf(buffer, "DISTANCE: %d",		&m_LevelData.s_Distance);
			sscanf(buffer, "BOSS: %s %d",			&m_LevelData.s_Boss, &m_LevelData.s_BossHP);
			sscanf(buffer, "SCENE: %s",				&m_LevelData.s_Scene);
			sscanf(buffer, "SHIP: %s",				&m_LevelData.s_Ship);
			sscanf(buffer, "BULLET: %s",			&m_LevelData.s_Bullet);
			sscanf(buffer, "%s %d %d %d",     &m_LevelData.s_Enemy,
																				&m_LevelData.s_Type,
																				&m_LevelData.s_HP,
																				&m_LevelData.s_Speed);
		}
	}

	fclose(fp);
	return 0;
}
And this is my text file:
LEVEL: 1

# Next level is the data path of the next level - duh! 
NEXT LEVEL: data/level_2

# Distance is how far the ship must travel until the boss is met
DISTANCE: 2

# Boss information goes here
# SYNTAX: directory, HP
BOSS: data/meteor		20

# Scene is pretty self explanatory
# NOTE: The bitmap must be 640x480 resolution or else it will be off. This will soon be fixed
SCENE: data/bg.bmp

# Ship and bullet are what the ship's image and it's bullet is
SHIP: data/ship
BULLET: data/bullet

# Enemy information here
# SYNTAX: enemy type (which enemy is it), HP, Speed,
data/enemy  1   3   4
data/enemy  2   4   5
  
How can I get it so that 1 and 2 are put in seperate parts in the s_Type array?
VG-Force | Ekim Gram Productions [edited by - Ekim_Gram on March 24, 2004 11:06:29 PM]

Share this post


Link to post
Share on other sites
Parse the file line by line and if an double entry is found it just creates a new item? I would stuff it all into a vector/list.

Toolmaker



-Earth is 98% full. Please delete anybody you can.

Share this post


Link to post
Share on other sites