473,386 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

reading a text file and returning enum

Ben
Hi all,

enum Colors defined as:
enum Colors{Red, Blue, Green};

I have a text file "colors.txt" with the following content, one per
line:
Red
Blue
Green.

My read() function is supposed to read an enumerated variable from
"colors.txt" file and to return a variable of type Colors. I'm not
sure how to do it.

My effort:

Colors read (ifstream inFile) {
char buffer[256];
while (!inFile.eof()) { //perhaps i don't even need this while
loop???
inFile.getline (buffer,100);
}
return (Colors(buffer)); // IT'S WRONG HERE!! ANY SUGGESTIONS??
}

Can anyone please show me the way..

Thanx for your time!
Ben
Jul 22 '05 #1
1 7739
Ben wrote:
enum Colors defined as:
enum Colors{Red, Blue, Green};

I have a text file "colors.txt" with the following content, one per
line:
Red
Blue
Green.

My read() function is supposed to read an enumerated variable from
"colors.txt" file and to return a variable of type Colors. I'm not
sure how to do it.
If the file contains three of them, one per line, how can the function
return only _one_ value?

My effort:

Colors read (ifstream inFile) {
char buffer[256];
while (!inFile.eof()) { //perhaps i don't even need this while
loop???
inFile.getline (buffer,100);
}
return (Colors(buffer)); // IT'S WRONG HERE!! ANY SUGGESTIONS??
}

Can anyone please show me the way..


If you need to convert a string into an enumerator, you have to write
the conversion yourself, there is no standard conversion defined for
'string -> enum' or vice versa.

Colors string2color(const std::string & s)
{
static std::pair<const char*, Colors> pairs[] =
{ { "Red", Red }, { "Blue", Blue }, { "Green", Green } };
for (int i = 0; i < sizeof pairs/sizeof pairs[0]; ++i)
{
if (s == pairs[i].first)
return pairs[i].second;
}
return Colors(777); // or any other value that doesn't represent
// a known color
}

Victor
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Row | last post by:
HI, I would first like to say its been about 3 years since looking at java im very rusty! I have to write a post it notes type applet which will function online. (reading from a flat text file)...
2
by: Roland Hall | last post by:
I have two(2) issues. I'm experiencing a little difficulty and having to resort to a work around. I already found one bug, although stated the bug was only in ODBC, which I'm not using. It...
6
by: KevinD | last post by:
assumption: I am new to C and old to COBOL I have been reading a lot (self teaching) but something is not sinking in with respect to reading a simple file - one record at a time. Using C, I am...
50
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem...
21
by: JoKur | last post by:
Hello, First let me tell you that I'm very new to C# and learning as I go. I'm trying to write a client application to communicate with a server (that I didn't write). Each message from the...
3
by: Luqman | last post by:
How can I retrieve text from html file and write to textbox. Best Regards, Luqman
9
by: Alex Buell | last post by:
I have a small text file which consist of the following data: ]] And the code I've written is as follows: ]] The trouble is, I can't work out why it goes into an infinite loop reading the...
9
by: Eric Lilja | last post by:
Hi! I have a program with a class that needs to be able to write itself to a file in clear text format. The file has two integers and vector of struct objects. The struct has a string that can...
9
by: szclark | last post by:
Hello, I am trying to extract information from a file I have so that I can answer some questions on it, the problem I'm having is that it is returning back mostly gibberish and I really don't know...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.