Hello,
I am new to your boards but am a little experienced in C++. I am having troubles grasping the concept of File IO, if anyone has a link to a good tutorial or can explain it to me I will be forever greatful. My main concert is saving floatpoint integers and character strings into a file and later restoring my defined objects with the data(serialization on a toned down scale).
Any help will be appreciated.
Here's some of my code to show how I am saving the data and to what it is saved:
CCharacter.h
#include "CDice.hpp"
#include <fstream.h>
#if !defined(CCHARACTER)
#define CCHARACTER
enum STATUS{ALIVE, DEAD};
class CCharacter{
public:
CCharacter();
CCharacter(char* pName, CDice* PDICE, int FLAG);
CCharacter(const char* pName, CDice* PDICE);
CCharacter(const char* pName, int* pArray);
~CCharacter(){;}
int GetMovement(){return Movement;}
int GetWeaponSkill(){return WeaponSkill;}
int GetBallisticSkill(){return BallisticSkill;}
int GetStrength(){return Strength;}
int GetToughness(){return Toughness;}
int GetWounds(){return Wounds;}
int GetInitiative(){return Initiative;}
int GetAttacks(){return Attacks;}
int GetDexterity(){return Dexterity;}
int GetLeadership(){return Leadership;}
int GetIntelligence(){return Intelligence;}
int GetCool(){return Cool;}
int GetWillPower(){return WillPower;}
int GetFellowship(){return Fellowship;}
char* GetName(){return NAME;}
STATUS GetStatus(){return Status;}
Wounded(int damageValue);//Damages the character by the value passed
Save(char* pFile);
protected:
CDice* pDice;//global CDice object
// CWeapon* pCurrentWeapon;
// CArmor* pCurrentArmor;
int Movement, WeaponSkill, BallisticSkill, Strength;
int Toughness, Wounds, Initiative, Attacks;
int Dexterity, Leadership, Intelligence, Cool;
int WillPower, Fellowship;
int aValues[14];
char NAME[30];
STATUS Status;//Status of character(ALIVE or DEAD)
int CharToInt(char letter);
};
#endif
CCharacter.cpp
#include <fstream.h>
#include "CCharacter.h"
#include <string.h>
CCharacter::CCharacter()
{
pDice = 0;
Movement = 1;
WeaponSkill = 1;
BallisticSkill = 1;
Strength = 1;
Toughness = 1;
Wounds = 1;
Initiative = 1;
Attacks = 1;
Dexterity = 1;
Leadership = 1;
Intelligence = 1;
Cool = 1;
WillPower = 1;
Fellowship = 1;
Status = ALIVE;
}
CCharacter::CCharacter(char* pName, CDice* PDICE, int FLAG)
{
if(!FLAG == 0)
{
char str[30];
ofstream output(pName,ios:ut);
output<<str;
strncpy(NAME, str, sizeof(NAME));
NAME[sizeof(NAME) - 1] = '\n';
output<<str;
cout<<str;
cout<<"\n name is "<<this->GetName();
}
else
{
cerr<<"Error has occured while opening file: "
<<pName;
return;
}
}
CCharacter::CCharacter(const char* pName, CDice* PDICE)
{
strncpy(NAME, pName, sizeof(NAME));
NAME[sizeof(NAME) - 1] = '\n';
pDice = PDICE;
Movement = pDice->Roll(3,1,2);
WeaponSkill = pDice->Roll(10,2,20);
BallisticSkill = pDice->Roll(10,2,20);
Strength = pDice->Roll(3,1,1);
Toughness = pDice->Roll(3,1,1);
Wounds = pDice->Roll(3,1,4);
Initiative = pDice->Roll(10,2,20);
Attacks = 1;
Dexterity = pDice->Roll(10,2,20);
Leadership = pDice->Roll(10,2,20);
Intelligence = pDice->Roll(10,2,20);
Cool = pDice->Roll(10,2,20);
WillPower = pDice->Roll(10,2,20);
Fellowship = pDice->Roll(10,2,20);
Status = ALIVE;
}
CCharacter::CCharacter(const char* pName, int* pArray)
{
strncpy(NAME, pName, sizeof(NAME));
NAME[sizeof(NAME) - 1] = '\n';
pDice = new CDice();
aValues[0] = Movement = pArray[0];
aValues[1] = WeaponSkill = pArray[1];
aValues[2] = BallisticSkill = pArray[2];
aValues[3] = Strength = pArray[3];
aValues[4] = Toughness = pArray[4];
aValues[5] = Wounds = pArray[5];
aValues[6] = Initiative = pArray[6];
aValues[7] = Attacks = pArray[7];
aValues[8] = Dexterity = pArray[8];
aValues[9] = Leadership = pArray[9];
aValues[10] = Intelligence = pArray[10];
aValues[11] = Cool = pArray[11];
aValues[12] = WillPower = pArray[12];
aValues[13] = Fellowship = pArray[13];
Status = ALIVE;
}
CCharacter::Wounded(int damageValue)
{
this->Wounds -= damageValue;
if(this->Wounds <= 0)
{
Status = DEAD;
}
}
CCharacter::Save(char* pFile)
{
ofstream inp(pFile, 0);
inp<<this->GetName()<<'\n';
inp<<this->GetMovement()<<'\n';
inp<<this->GetWeaponSkill()<<'\n';
inp<<this->GetBallisticSkill()<<'\n';
inp<<this->GetStrength()<<'\n';
inp<<this->GetToughness()<<'\n';
inp<<this->GetWounds()<<'\n';
inp<<this->GetInitiative()<<'\n';
inp<<this->GetAttacks()<<'\n';
inp<<this->GetDexterity()<<'\n';
inp<<this->GetLeadership()<<'\n';
inp<<this->GetIntelligence()<<'\n';
inp<<this->GetCool()<<'\n';
inp<<this->GetWillPower()<<'\n';
inp<<this->GetFellowship()<<'\n';
}
CCharacter::CharToInt(char letter)
{
return ((int)letter - 48);
}
CCdice.h
#if !defined(CDICE)
#define CDICE
/************************************************** ******************/
//This is the dice class used for generating random numbers based on
//the values the user inputs. The function Roll(sides,times,modifier
//, and flag are used to specify the range of values generated.
//Sides is equal to the maximum number allowed. Times is equal to the
//number of times a number is generated and added to the rolling total
//modifier is equal to a number to be added to the number generated.
//And finally flag is used to determine if the number is the lowest
//or highest out of the times the number is generated from the range
//allowed. NONE is the default, this setting keeps a rolling total.
/************************************************** ******************/
#include <fstream.h>
enum FLAG{NONE, LARGEST, SMALLEST};
class CDice {
public:
CDice();
~CDice(){};
virtual int Roll(int sides, int times = 1, int modifier = 0,
FLAG flag = NONE);
int GetGenerated(); //Returns the Generated value.
protected:
void SetGenerated(int test){Generated = test;}
int Generated; //Used to flag if the srand function has been
//called.
virtual int GetRandom(int sides);
};
#endif
CDice.cpp
#include "CDice.hpp"
#include <stdlib.h>
#include <time.h>
#include <fstream.h>
//Main function used to generated random numbers.
CDice::Roll(int sides, int times,int modifier,FLAG flag)
{
int total =0;
int* parray = new int[times];
switch(flag)
{
case NONE:
{
for(int i=0;i<times;i++)
{
total += this->GetRandom(sides);
}
total += modifier;
delete [] parray;
break;
}
case SMALLEST:
{
int i = 0;
for(i=0;i<times;i++)
{
parray[i] = this->GetRandom(sides);
}
total = parray[0];
for(i=0;i<times;i++)
{
if(total > parray[i])
total = parray[i];
}
total += modifier;
delete [] parray;
break;
}
case LARGEST:
{
int i = 0;
for(i=0;i<times;i++)
{
parray[i] = this->GetRandom(sides);
}
for(i=0;i<times;i++)
{
if(total < parray[i])
total = parray[i];
}
total += modifier;
delete [] parray;
break;
}
default:
{
total = 1010;
delete [] parray;
break;
}
}
return total;
}
CDice::GetRandom(int sides)
{
if(!Generated)
{
srand( (unsigned)time( NULL ) );
this->SetGenerated(true);
}
int total = 0, temp = 0;
temp = rand();
temp = (temp % sides) +1;
total +=temp;
return total;
}
//Returns the value of Generated.
CDice::GetGenerated()
{
return Generated;
}
//Default constructor.
CDice::CDice():Generated(false)
{}
/************************************************** ******************/
Test.cpp(Main function for testing)
include "CDice.hpp"
#include "CCharacter.h"
int CharToInt(char);
int main()
{
CDice* PDICE = new CDice();
CCharacter Jeramy("Jeramy", PDICE, 1);
Jeramy.Save(Jeramy.GetName());
/*
int Num;
char Char[]="34";
Num = ((int)Char[1] - 48);
cout<<Num<<'\n';
Num = CharToInt(Char[0]);
cout<<Num<<'\n';
*/
return 0;
}
int CharToInt(char letter)
{
return ((int)letter - 48);
}