Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to write a enum value to a file

Status
Not open for further replies.

aeaswara

Programmer
Mar 11, 2004
1
US
Hi all,

I need to know how will you write a enum value to an output file.
The Program Is As Follows:
typedef enum {
ID,INTLITERAL, BEGIN, END, READ, WRITE
}token;

when i read a "begin" in an input file say, in.txt i return BEGIN. and after returing i want to write "BEGIN" to a output file, say out.txt.
how do i do it.
thanks for helping.
aeaswara.
 
enum is an integral (integer) type, but its sizeof is not specified by the language standard. It seems safe enum i/o approach is to force (int) cast when writing the enum value (and vice versa on reading)...
 
If you're looking for enum constant to string conversion, then you have to do something like this
Code:
char *tokenstr[] = {
   "ID", "INTLITERAL", "BEGIN", "END", "READ", "WRITE"
};

...

printf("%s\n", tokenstr[token] );

But this only works best if you don't have any explicit assignments in your original enumeration. For example, this would break the above.

Code:
typedef enum {
    ID, INTLITERAL, BEGIN=10, END, READ=20, WRITE
}token;

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top