473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Better way to use istream to read an ascii value into a char.

In one of my files I am outputting the value of a char into a human readable
file. That is,
char a = 123;
std::ofstream 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?
May 4 '06 #1
5 2356
Jim Langston wrote:
In one of my files I am outputting the value of a char into a human readable
file. That is,
char a = 123;
std::ofstream 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?


Any easy way would be to make your own stream:

class my_stream
{
public:
my_stream(const std::string& fn)
: ifs_(fn.c_str())
{
}

char read_byte()
{
int i = 0;
ifs_ >> i;
char c = i;
return c;
}

private:
std::ifstream ifs_;
};

my_stream& operator>>(my_stream& s, char& c)
{
c = s.read_byte();
return s;
}

int main()
{
my_stream s("test");
char c;
s >> c;
}

Deriving from std::*istream or aggregating one depends on your design.
Jonathan

May 4 '06 #2
Jim Langston wrote:
In one of my files I am outputting the value of a char into a human readable
file. That is,
char a = 123;
std::ofstream 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;
[snip]
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?


Wrap a char reference in a user defined type and define operator>> for
this type:

#include <iostream>

struct achar
{
achar(char &c) : c_(c) {}
char &c_;
};

std::istream &
operator>>(std::istream &is, achar a)
{
int tmp;
is >> tmp;
a.c_ = tmp;
return is;
}

int
main()
{
char c;
std::cin >> achar(c);
std::cout << c << std::endl;
}

May 4 '06 #3
"Markus Schoder" <a3*************@yahoo.de> wrote in message
news:11**********************@y43g2000cwc.googlegr oups.com...
Jim Langston wrote:
In one of my files I am outputting the value of a char into a human
readable
file. That is,
char a = 123;
std::ofstream 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;
[snip]
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.


<snip>
Wrap a char reference in a user defined type and define operator>> for
this type:

#include <iostream>

struct achar
{
achar(char &c) : c_(c) {}
char &c_;
};

std::istream &
operator>>(std::istream &is, achar a)
{
int tmp;
is >> tmp;
a.c_ = tmp;
return is;
}

int
main()
{
char c;
std::cin >> achar(c);
std::cout << c << std::endl;
}


Ahh, perfect, thanks! I'm sure you meant
std::istream &
operator>>(std::istream &is, achar& a)
May 6 '06 #4

"Jim Langston" <ta*******@rocketmail.com> wrote in message
news:P0**************@fe04.lga...
"Markus Schoder" <a3*************@yahoo.de> wrote in message
news:11**********************@y43g2000cwc.googlegr oups.com...

[big snip]
Wrap a char reference in a user defined type and define operator>> for
this type:

#include <iostream>

struct achar
{
achar(char &c) : c_(c) {}
char &c_;
};

std::istream &
operator>>(std::istream &is, achar a)
{
int tmp;
is >> tmp;
a.c_ = tmp;
return is;
}

int
main()
{
char c;
std::cin >> achar(c);
std::cout << c << std::endl;
}


Ahh, perfect, thanks! I'm sure you meant
std::istream &
operator>>(std::istream &is, achar& a)


I guess I was wrong on this. I tried it with the achar& a and the compiler
warned about converting an achar a to an achar& a on a non constant
variable. So I put it the way you had it and it seems to work (compiled,
only one warning about achar not being able to have an assigment oporator
generated) but not tested. Thanks again.
May 6 '06 #5
Jim Langston wrote:
"Jim Langston" <ta*******@rocketmail.com> wrote in message
news:P0**************@fe04.lga...
"Markus Schoder" <a3*************@yahoo.de> wrote in message
news:11**********************@y43g2000cwc.googlegr oups.com... [big snip]
Wrap a char reference in a user defined type and define operator>> for
this type:

#include <iostream>

struct achar
{
achar(char &c) : c_(c) {}
char &c_;
};

std::istream &
operator>>(std::istream &is, achar a)
{
int tmp;
is >> tmp;
a.c_ = tmp;
return is;
}

int
main()
{
char c;
std::cin >> achar(c);
std::cout << c << std::endl;
}


Ahh, perfect, thanks! I'm sure you meant
std::istream &
operator>>(std::istream &is, achar& a)


I guess I was wrong on this.


Note that achar takes the char by reference, not by value. So whether
achar itself is passed by value or reference does not matter.
I tried it with the achar& a and the compiler
warned about converting an achar a to an achar& a on a non constant
variable.


In the statement

std::cin >> achar(c);

"achar(c)" constructs an rvalue which cannot be bound to a non-const
reference. You must either pass it by value or by const reference.
Jonathan

May 6 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Steve | last post by:
Hi, I'm trying to convert a file reading loop into one using streams. The BSD OS read API returns the number of bytes read, but istream::read returns itself. How can I find out the number of...
3
by: matthurne | last post by:
I'm doing a chapter 12 exercise from Accelerated C++ ... writing a string-like class which stores its data in a low-level way. My class, called Str, uses a char array and length variable. I've...
3
by: lpe540 | last post by:
Hi, I'm having trouble using istream to read in a file in its entirety on UNIX. I've written a dummy program that essencially reads in a file from stdin and writes it out to a file. When I cat a...
6
by: Jason K | last post by:
Let me preface this by saying this obviously isn't a C++ *language* issue per se; rather probably an issue relating to quality of implementation, unless I'm just misusing iostream... I wrote a...
13
by: Peteroid | last post by:
These don't work (I'm using VS C++.NET 2005 Express with clr:/pure syntax): ostream& operator <<( ostream& output, String^ str ) { output << str ; //compile error return output ; } ...
1
by: mwebel | last post by:
Hi, My Module (B) needs to read from a istream (provided by another module A) and again the module (C) i use accepts only char*. So actually it accepts filenames. But i dont want to store the...
21
by: Peter Larsen [] | last post by:
Hi, I have a problem using System.Runtime.InteropServices.ComTypes.IStream. Sample using the Read() method: if (IStreamObject != null) { IntPtr readBytes = IntPtr.Zero;...
4
by: =?Utf-8?B?Sm9obg==?= | last post by:
Hi all, I am developing website application in asp.net , visual C# and atl com. I am using atl com component in visual C# application. One of the function of com component interface returns...
4
by: james.lawton | last post by:
Hi, I'm having a problem that I can't diagnose. I'm creating istreams of unknown types, and want to manage them on the stack, co I'm passing around ownership-holding pointers. Usually, I would...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.