J
Jim Langston
In one of my files I am outputting the value of a char into a human readable
file. That is,
char a = 123;
std:fstream CharFile( ("Players\\" + Name + ".char").c_str());
if ( CharFile.is_open() )
CharFile << (int) a;
So the file has the character a stored as "123".
That was the easy part, now comes the fun of reading it back into the char.
I tried a number of things and finally wound up doing this:
std::istream& operator >>( std::istream& is, char& Byte )
{
int temp;
is >> temp;
Byte = temp;
return is;
}
char a;
std::ifstream CharFile( ( "Players\\" + CharName + ".char" ).c_str());
if ( CharFile.is_open() )
CharFile >> a;
You may wonder why I did that instead of just directly reading it into a
temp int directly and assigning, the reason being I'm writing many of these
to the file.
is >> CChar.GM >> CChar.GMLevel >> CChar.Banned >> CChar.Jailed >>
CChar.Muted >>
CChar.MessageRange >> CChar.Avatar >> CChar.Map >> CChar.Pos.x >>
CChar.Pos.y >>
CChar.Pos.z >> CChar.X_Degrees >> CChar.Race >> CChar.Vigor >>
CChar.Fortitude >>
CChar.Coordination >> CChar.Agility >> CChar.Reason >> CChar.Perception >>
CChar.Willpower >> CChar.Psychic >> CChar.Charisma >> CChar.Affinity >>
CChar.AffinityAir >> CChar.AffinityWater >> CChar.AffinityFire >>
CChar.AffinityEarth >> CChar.Sin;
If I had to break this up into different calls it would be, in my opionion,
harder to maintain.
The thing I don't like about overriding the operator >> for a char, though,
is if I ever want to actually write a char as a byte itself (which I don't
think I will).
I'm just wondering if there is a better way.
I attempted something along the lines of:
std::istream& ReadByteVal( std::istream& is, char& Byte ) {/**/}
and then attempted to use it like
is >> ReadByteVal( is, CChar.Agility ) >> ReadByteVal( is, CChar.Reason ) >>
/**/
but that wouldn't compile, the compiler complaining something about not
finding a call taking an rvalue.
Someone else came up with a horrible looking template that seems to be prone
to errors itself. Anyone have any ideas?
file. That is,
char a = 123;
std:fstream CharFile( ("Players\\" + Name + ".char").c_str());
if ( CharFile.is_open() )
CharFile << (int) a;
So the file has the character a stored as "123".
That was the easy part, now comes the fun of reading it back into the char.
I tried a number of things and finally wound up doing this:
std::istream& operator >>( std::istream& is, char& Byte )
{
int temp;
is >> temp;
Byte = temp;
return is;
}
char a;
std::ifstream CharFile( ( "Players\\" + CharName + ".char" ).c_str());
if ( CharFile.is_open() )
CharFile >> a;
You may wonder why I did that instead of just directly reading it into a
temp int directly and assigning, the reason being I'm writing many of these
to the file.
is >> CChar.GM >> CChar.GMLevel >> CChar.Banned >> CChar.Jailed >>
CChar.Muted >>
CChar.MessageRange >> CChar.Avatar >> CChar.Map >> CChar.Pos.x >>
CChar.Pos.y >>
CChar.Pos.z >> CChar.X_Degrees >> CChar.Race >> CChar.Vigor >>
CChar.Fortitude >>
CChar.Coordination >> CChar.Agility >> CChar.Reason >> CChar.Perception >>
CChar.Willpower >> CChar.Psychic >> CChar.Charisma >> CChar.Affinity >>
CChar.AffinityAir >> CChar.AffinityWater >> CChar.AffinityFire >>
CChar.AffinityEarth >> CChar.Sin;
If I had to break this up into different calls it would be, in my opionion,
harder to maintain.
The thing I don't like about overriding the operator >> for a char, though,
is if I ever want to actually write a char as a byte itself (which I don't
think I will).
I'm just wondering if there is a better way.
I attempted something along the lines of:
std::istream& ReadByteVal( std::istream& is, char& Byte ) {/**/}
and then attempted to use it like
is >> ReadByteVal( is, CChar.Agility ) >> ReadByteVal( is, CChar.Reason ) >>
/**/
but that wouldn't compile, the compiler complaining something about not
finding a call taking an rvalue.
Someone else came up with a horrible looking template that seems to be prone
to errors itself. Anyone have any ideas?