Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


How do I read in an enum value from a file?

Posted on 2007-08-11
Medium Priority
Last Modified: 2010-04-01
Hi Experts,

In my VS native c++ console program, I define an enum e.g. Color {RED, GREEN, BLUE}.  This is logged to an ofstream such as:

ofstream my_ofs("blah");
Color my_color = RED;
my_ofs << my_color;

No problem.  Later I need to read in this value. I'd like to do something like this:

Color your_color;
ifstream my_ifs("blah");
my_ifs >> your_color;

This does not work because the compiler doesn't know how to read in a Color type.  But it's just an int in actuality, so it seems that something like this _should_ work. Any suggestions on the correct way to do this?

I don't care too much if the file actually stores the text "RED" or the integer code for it. However, the former would be preferred (though, that way probably entails much more code).

Question by:highqllc

Accepted Solution

Dimkov earned 1000 total points
ID: 19675924
can you read int

int a;
your_color = (Color)a;

Author Comment

ID: 19675937
Right!  I suspected there was a simple way.  Thanks.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

576 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question