Jump to content
  • Advertisement


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


More file reading problems

This topic is 5226 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);
    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,

	return 0;
And this is my text file:

# 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

# 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.


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

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!