I am trying to write data to a file so that it can be loaded in the next time around...when I save to file...it works. But when I read from the same file and then try to save...the char * data is corrupted. I though my program was closing before it could save all the date but that is not the case; tried a sleep( 10 ) before exiting the program.
Here is the code for loading from the file...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
Screen *Pro::LoadScreenFromFile( ifstream &aStream )
{
ReportMessage( "Pro: Loading screen from file..." );
string Command = "";
string NameID = "";
string FileID = "";
float PosX = 0.0f;
float PosY = 0.0f;
int NumberOfButtons = 0;
Screen *aTempScreen = new Screen( );
//Read IDs Into Strings
aStream >> NameID;
aStream >> FileID;
printf( "Pro: Screen -> Name ID: %s\n", NameID.c_str( ) );
printf( "Pro: Screen -> File ID: %s\n", FileID.c_str( ) );
//Setup Screen IDs
aTempScreen->SetNameID( NameID );
aTempScreen->SetFileID( FileID );
}
Button *Pro::LoadButtonFromFile( ifstream &aStream )
{
ReportMessage( "Pro: Loading button from file..." );
string NameID = "";
string FileID = "";
string Command = "";
int Width = 0;
int Height = 0;
int OffsetX = 0;
int OffsetY = 0;
Button *aTempButton = new Button( );
aStream >> NameID;
aStream >> FileID;
printf( "Pro: Button -> Name ID: %s\n", NameID.c_str( ) );
printf( "Pro: Button -> File ID: %s\n", FileID.c_str( ) );
aTempButton->SetNameID( NameID );
aTempButton->SetFileID( FileID );
}
|
Here is the code for saving to the file...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
void Screen::Save( ofstream &aStream )
{
ReportMessage( "Pro: Saving screen to file..." );
char *NameID = const_cast<char*>( GetNameID( ) );
char *FileID = const_cast<char*>( GetFileID( ) );
printf( "Pro: Screen -> Name ID: %s\n", NameID );
printf( "Pro: Screen -> File ID: %s\n", FileID );
aStream << ".screen ";
aStream << NameID << " ";
aStream << FileID << " ";
}
void Button::Save( ofstream &aStream )
{
ReportMessage( "Pro: Saving button to file..." );
char *NameID = const_cast<char*>( GetNameID( ) );
char *FileID = const_cast<char*>( GetFileID( ) );
printf( "Pro: Button -> Name ID: %s\n", NameID );
printf( "Pro: Button -> File ID: %s\n", FileID );
aStream << ".button ";
aStream << NameID << " ";
aStream << FileID << " ";
}
|
GetNameID( ) & GetFileID( ) return char *
Console Output -- Working Version
In this version, I am just setting values in the main.cpp and then saving them to the file.
------------------------------------------------------
Pro: Constructing Pro Engine...
Pro: Constructing null screens to fill array...
Pro: Done.
Pro: Constructing texture...
Pro: Constructing screen...
Pro: Done.
Pro: Setting properties of Pro Engine...
Pro: Loading fonts to engine...
Pro: Loading font with offset...
Pro: Texture -> File ID: PixelFont.bmp
Pro: Done.
Pro: Loading font with offset...
Pro: Texture -> File ID: PixelFont.bmp
Pro: Done.
Pro: Loading font with offset...
Pro: Texture -> File ID: PixelFont.bmp
Pro: Done.
Pro: Loading font with offset...
Pro: Texture -> File ID: PixelFont.bmp
Pro: Done.
Pro: Loading font with offset...
Pro: Texture -> File ID: PixelFont.bmp
Pro: Done.
Pro: Loading font with offset...
Pro: Texture -> File ID: PixelFont.bmp
Pro: Done.
Pro: Loading font with offset...
Pro: Texture -> File ID: PixelFont.bmp
Pro: Done.
Pro: Loading font with offset...
Pro: Texture -> File ID: PixelFont.bmp
Pro: Done.
Pro: Texture -> File ID: Font.bmp
Pro: Texture -> File ID: Particle.bmp
Pro: Loading texture with alpha channel...
Pro: Texture -> File ID: AmbientDust.bmp
Pro: Done.
Pro: Setting blend state.
Pro: Done.
Pro: Loading texture with alpha channel...
Pro: Texture -> File ID: Clouds2.bmp
Pro: Done.
Pro: Setting blend state.
Pro: Done.
Pro: Setting blend state.
Pro: Done.
Pro: Loading texture...
Pro: Texture -> File ID: Background.bmp
Pro: Done.
Pro: Setting blend state.
Pro: Done.
Pro: Attaching screen to engine...
Pro: Loading texture...
Pro: Texture -> File ID: 9Patch.bmp
Pro: Done.
Pro: Done.
Pro: Finding font...
Pro: Font was found.
Pro: Beginning to attach button to screen...
Pro: Loading texture...
Pro: Texture -> File ID: Button.bmp
Pro: Done.
Pro: Attaching button to screen...
Pro: Done.
Pro: Done.
~ Project closed by user...
Pro: Saving game to file...
Pro: Saving screen to file...
Pro: Screen -> Name ID: TestScreen
Pro: Screen -> File ID: 9Patch.bmp
Pro: Saving buttons inside screen...
Pro: Saving button to file...
Pro: Screen -> Name ID: Test
Pro: Screen -> File ID: Button.bmp
Pro: Done saving button to file.
Pro: Done saving screen to file.
Pro: Done saving game to file.
Console Output -- Non-Working Version
In this version, I am loading the values from the file and then saving them when the program is closed.
------------------------------------------------------
Pro: Constructing Pro Engine...
Pro: Constructing null screens to fill array...
Pro: Done.
Pro: Constructing texture...
Pro: Constructing screen...
Pro: Done.
Pro: Setting properties of Pro Engine...
Pro: Loading fonts to engine...
Pro: Loading font with offset...
Pro: Texture -> File ID: PixelFont.bmp
Pro: Done.
Pro: Loading font with offset...
Pro: Texture -> File ID: PixelFont.bmp
Pro: Done.
Pro: Loading font with offset...
Pro: Texture -> File ID: PixelFont.bmp
Pro: Done.
Pro: Loading font with offset...
Pro: Texture -> File ID: PixelFont.bmp
Pro: Done.
Pro: Loading font with offset...
Pro: Texture -> File ID: PixelFont.bmp
Pro: Done.
Pro: Loading font with offset...
Pro: Texture -> File ID: PixelFont.bmp
Pro: Done.
Pro: Loading font with offset...
Pro: Texture -> File ID: PixelFont.bmp
Pro: Done.
Pro: Loading font with offset...
Pro: Texture -> File ID: PixelFont.bmp
Pro: Done.
Pro: Texture -> File ID: Font.bmp
Pro: Texture -> File ID: Particle.bmp
Pro: Loading texture with alpha channel...
Pro: Texture -> File ID: AmbientDust.bmp
Pro: Done.
Pro: Setting blend state.
Pro: Done.
Pro: Loading texture with alpha channel...
Pro: Texture -> File ID: Clouds2.bmp
Pro: Done.
Pro: Setting blend state.
Pro: Done.
Pro: Setting blend state.
Pro: Done.
Pro: Loading texture...
Pro: Texture -> File ID: Background.bmp
Pro: Done.
Pro: Setting blend state.
Pro: Done.
Pro: Loading game from file...
Pro: Loading screen from file...
Pro: Constructing screen...
Pro: Done.
Pro: NameID - TestScreen, FileID - 9Patch.bmp
Pro: Loading texture...
Pro: Texture -> File ID: 9Patch.bmp
Pro: Done.
Pro: Loading buttons inside screen...
Pro: Loading button from file...
Pro: Loading texture...
Pro: Texture -> File ID: Button.bmp
Pro: Done.
Pro: Done loading button from file.
Pro: Attaching button to screen...
Pro: Done.
Pro: Done loading screen from file.
Pro: Attaching screen to engine...
Pro: Done.
Pro: Loading blank command...
Pro: Done loading game from file.
~ Project closed by user...
Pro: Saving game to file...
Pro: Saving screen to file...
Pro: Screen -> Name ID:
Pro: Screen -> File ID: 9Pat ?
Pro: Saving buttons inside screen...
Pro: Saving button to file...
Pro: Screen -> Name ID:
Pro: Screen -> File ID: U??V?
Pro: Done saving button to file.
Pro: Done saving screen to file.
Pro: Done saving game to file.
Now the problem has to occur after I load the screen and button from the file...cause from the console output, the filenames are read in correct and the textures are loaded properly. Why does it save the file incorrectly here?