Fstream + Enum's

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Donutslayer7
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 16
Joined: Tue Aug 18, 2009 5:16 pm
Current Project: Map-Editor
Favorite Gaming Platforms: N64, SNES, anything Nintendo
Programming Language of Choice: C++
Location: U.S.

Fstream + Enum's

Post 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
X Abstract X
Chaos Rift Regular
Chaos Rift Regular
Posts: 173
Joined: Thu Feb 11, 2010 9:46 pm

Re: Fstream + Enum's

Post 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"
User avatar
Donutslayer7
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 16
Joined: Tue Aug 18, 2009 5:16 pm
Current Project: Map-Editor
Favorite Gaming Platforms: N64, SNES, anything Nintendo
Programming Language of Choice: C++
Location: U.S.

Re: Fstream + Enum's

Post by Donutslayer7 »

Okay, thanks
Post Reply