Trouble with reading from a file

Started by
4 comments, last by Ekim_Gram 20 years, 1 month ago
Ok, since the replies from my recent topic were such a bit help, I'm hoping I'd get the same here. I took some of your advice and decided to have my level data put into a binary file. I've run into some problems though. I have a struct and a class. The struct is where the level data is read into and the class has a declaration of the struct and some other functions to load the level and such.
struct Level_Data
{
	int   s_Level;		// Current level
	int   s_Distance; // Distance until boss
	char* s_Boss;     // Boss directory
};

class CLevel
{
public:
	int InitLevel(char *dir);	// Initialize the level

	Level_Data m_LevelData;		// The level data
};
    
And so far I have the InitLevel() funciton like so:

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, "r")) == 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 three lines of the file to set the current level, distance needed and the boss directory
	fgets(buffer, 255, fp);
        sscanf(buffer, "LEVEL: %d", &m_LevelData.s_Level);
	sscanf(buffer, "DISTANCE: %d", &m_LevelData.s_Distance);
	sscanf(buffer, "BOSS: %s", m_LevelData.s_Boss);

	fclose(fp);
	return 0;
}
    
And last but not least, I have my binary file (or what it's made of so far):

LEVEL: 1
DISTANCE: 10
BOSS: data/meteor
    
In my main loop, I have a function that prints out the data stored in s_Distance, s_Level, and s_Boss. My problem is that when s_Boss is printed to the screen (the data path extracted from the binary file) it only prints out (null) VG-Force | Ekim Gram Productions [edited by - Ekim_Gram on March 23, 2004 7:58:48 PM] [edited by - Ekim_Gram on March 23, 2004 7:59:32 PM] [edited by - Ekim_Gram on March 23, 2004 8:27:01 PM]
Advertisement
I haven't used sscanf much, but I think the problem is that you need to give the char pointer a size before you try to load it in. Personally I think you'd have much more luck just sticking with fread(..)

EDIT : btw - you are using an ascii file, not binary

-Nate S.

[edited by - nstrg on March 23, 2004 8:07:27 PM]
"Always forgive your enemies, nothing annoys them more.."
u should use fopen(filename, "rb")

the "rb" tells it to read from a binary file, without the "b" ur prettymuch screwed on windows platforms


Cartman''s definition of sexual harrasement:
"When you are trying to have intercourse with a lady friend, and some other guy comes up and tickles your balls from behind"

(watch South Park, it rocks)
Cartman's definition of sexual harrasement:"When you are trying to have intercourse with a lady friend, and some other guy comes up and tickles your balls from behind"(watch South Park, it rocks)
Nope, the "rb" doesn't do it either. Still gettting (null) as the output.

And I also tried using char s_Boss[255] but then all I get is a blank space.

EDIT: Well, I dunno what I did but I fooled around with the code for a bit and got it to work. Thanks guys!


VG-Force | Ekim Gram Productions

[edited by - Ekim_Gram on March 23, 2004 8:29:08 PM]

[edited by - Ekim_Gram on March 23, 2004 8:48:46 PM]
Can't you just save all of the hassle and do something like this:

//--To save the data just use:
fwrite(m_LevelData, 1, sizeof(Level_Data), fp);

//--To read the data just use:
fread(m_LevelData, 1, sizeof(Level_Data), fp);


-UltimaX-
Ariel Productions
|Designing A Screen Shot System|

"You wished for a white christmas... Now go shovel your wishes!"

[edited by - UltimaX on March 23, 2004 9:09:51 PM]
BTW:
Here's a test I did and it ran fine:
#include "windows.h"struct Level_Data{		int  s_Level;			int  s_Distance; 	char s_Boss[256];};//int main(int argc, char* argv[]){	//--Write Test	//--	FILE* File = fopen("Test.txt", "wb");	if(File == NULL)	{		return 0;	}//	Level_Data* Data = new Level_Data;	if(Data == NULL)	{		fclose(File);		return 0;	}//	sprintf(Data->s_Boss, "Demon Slayer");        Data->s_Boss[strlen(Data->s_Boss)] = '\0';	Data->s_Distance = 1000;	Data->s_Level = 99;//	fwrite(Data, 1, sizeof(Level_Data), File);	fclose(File);	if(Data)	{		delete Data;		Data = NULL;	}//	//--Read Test	//--	File = fopen("Test.txt", "rb");	if(File == NULL)	{		return 0;	}//	Data = new Level_Data;	if(Data == NULL)	{		fclose(File);		return 0;	}//	fread(Data, 1, sizeof(Level_Data), File);	Data->s_Boss[strlen(Data->s_Boss)] = '\0';//	printf("BOSS    : %s\n"	       "LEVEL   : %d\n"	       "DISTANCE: %d\n", Data->s_Boss, Data->s_Level, Data->s_Distance);	fclose(File);	if(Data)	{		delete Data;		Data = NULL;	}	return 0;}


-UltimaX-
Ariel Productions
|Designing A Screen Shot System|

"You wished for a white christmas... Now go shovel your wishes!"

[edited by - UltimaX on March 23, 2004 9:22:56 PM]

This topic is closed to new replies.

Advertisement