Page 1 of 1

Fstream + Enum's

Posted: Sun Aug 15, 2010 4:07 pm
by Donutslayer7
Can you read in a word from a file with Fstream, but instead of it being read as a string, it's read as the number of an enumeration(if that made any sense).

Code: Select all

//somewhere in main.cpp
enum Colors
{
     red, blue, green
};
Colors myColor;
char[20] skip;
void read_file()
{
     myFile << skip;
     myFile << myColor;
}
and somewhere in the file

Code: Select all

Color: red

Re: Fstream + Enum's

Posted: Sun Aug 15, 2010 4:38 pm
by X Abstract X
I don't think it's possible. You could try using a hashmap, personally I use boost::unordered_map for things like this. It won't be as fast as an enum but it's a lot faster than std::map. The boost libraries offer so much, it's worth adding it as a dependency anyway. If you want to use boost, here's a little demo:

Code: Select all

boost::unordered_map<std::string, unsigned int> colorHash;
colorHash.insert(std::pair<std::string, unsigned int>("blue", 0));

boost::unordered_map<std::string, unsigned int>::const_iterator i = colorHash.find("blue");

if (i != colorHash.end())
    std::cout << i->first << " = " << i->second << "\n"; //prints: "blue = 0"

Re: Fstream + Enum's

Posted: Sun Aug 15, 2010 4:58 pm
by Donutslayer7
Okay, thanks