473,467 Members | 1,410 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

file io and enumerations

How can I read and write an enum value to a file?

The code in question:

Cell temp;
temp.m_x = x;
temp.m_y = y;
file >> temp.m_type;

The error:
c:\Documents and Settings\christopher.pisz\My Documents\Visual Studio
Projects\parser\maze.cpp(108): error C2679: binary '>>' : no operator
found which takes a right-hand operand of type 'Cell::CELLTYPE' (or
there is no acceptable conversion)

The Cell defination:

class Cell
{
public:
enum CELLTYPE{ START, PASSABLE, IMPASSABLE, END, UNKNOWN};

Cell();
~Cell();

void Display();

int m_x,
m_y;
CELLTYPE m_type;
};

file is of course an ifstream declared and opened earlier...

At first I wanted to have the enumerated names in the input file so
that it would be reader friendly, then I tryed just using the number
values after I gave up on converting strings to enumerated values using
a long google search and not liking the ideas presented. Now I can't
even seem to read in numbers to an enumerated type. enum is seeming
less and less friendly. I thought the whole idea of enum was to make
things friendly to the coder and reader...

What is your ideas on a solution to this problem? Besides the obvious
of not using enum at all, but I don't like that, because m_type is not
an int, it is a cell type.

Is there an effiicient way to read in strings from the file such as
START, PASSABLE, etc. and convert them to an enumerated type?

If not, is there a way to just read in a number such as 0 and convert
it to the enumerated type START without using a switch statement and
having to edit it every time a new type is introduced?

Jun 28 '06 #1
1 1521
cp***@austin.rr.com wrote:
How can I read and write an enum value to a file?

You can just write it and provide a wrapper operator to read it:

#include <string>
#include <iostream>

enum E { A, B };

std::istream& operator>>( std::istream& in, E& e )
{
unsigned tmp;

in >> tmp;

e = static_cast<E>(tmp);

return in;
}

int main()
{
E e = A;

std::cin >> e;
}

--
Ian Collins.
Jun 28 '06 #2

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

Similar topics

1
by: Joyce | last post by:
In my schema I have 2 enumerations, let's say, country description and country code, and I want to use them so I can map each country description to its precise country code (and no other). So far...
0
by: Plinkerton | last post by:
I'm making an Base Class that will be inherited. In my base class, I have a public enumeration that defines a list of things I want my class to be able to do. I use it for Method input...
21
by: Christopher Benson-Manica | last post by:
I'll try to explain what I want to do: I have foo.h and foo.cpp. Units that include foo.h will define an enumeration bar: enum bar { typeNone, typeBaz, typeQuux, ... , count }; A method...
3
by: JoeH | last post by:
Hi, I'm using a COM DLL (created in VB) in my javascript code and can successfully call its methods and get/set its properties. There are also some Public enumerations defined in the ActiveX...
5
by: Seamus M | last post by:
I can't find any info on enumerations in the PHP manual, so I assume there is no built in way to create them. Can anyone tell me the best way to build a simple enumeration, such as: Enum...
1
by: someone else | last post by:
I have some code that creates dynamic enumerations for use in a PropertyGrid control. This all works perfectly but the memory usage of the program increases quite quicly when viewing the...
4
by: ChrisB | last post by:
Hello: I will be creating 50+ enumerations related to a large number of classes that span a number of namespaces. I was wondering if there are any "best practices" when defining enumerations. ...
27
by: Ben Finney | last post by:
Antoon Pardon wrote: > I just downloaded your enum module for python > and played a bit with it. IMO some of the behaviour makes it less > usefull. Feedback is appreciated. I'm hoping to...
77
by: Ben Finney | last post by:
Howdy all, PEP 354: Enumerations in Python has been accepted as a draft PEP. The current version can be viewed online: <URL:http://www.python.org/peps/pep-0354.html> Here is the...
3
by: wild kat | last post by:
I was wondering if someone could guide me or give me few pointers on how to go about doing this. parsing a C++ header file. While parsing , I want to extract enumerations from the file. I would...
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
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.