By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,631 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

file io and enumerations

P: n/a
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


void Display();

int m_x,
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
Share this Question
Share on Google+
1 Reply

P: n/a
cp*** 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 discussion thread is closed

Replies have been disabled for this discussion.