reading text files!
hi, currently i have no problem retrieving each line in the text file using the following code:
char buffer[256];
ifstream myList("List.txt");
if (! examplefile.is_open())
{ cout << "Error opening file"; exit (1); }
while (! myList.eof() )
{
myList.getline (buffer,100);
cout << buffer << endl;
}
However, im trying to abstract each value separated by a comma. heres an example of a line in the text file
"mr john", 42, 8, "likes banana", true
"miss john", 40, 0, "likes papaya", false
i would like to do something tp retrieve the values and store them like this...
Name = mr john;
age = 42;
workingHrs = 8;
f_food = "likes banana;
active = true;
thanks in advance.
[819 byte] By [
norphos] at [2007-11-19 6:09:59]

# 1 Re: reading text files!
The default delimiter for istream::getline() is '\n'. You can specify ',' as the delimiter. It will be easier if your data is without the double quotes, you can just use myList.getline(str, maxlen, ','); for reading the name. Then you can get the integer by myList>>age; Similarly you can get teh rest of the data, and the last string (true/false) with the >> operator.
# 2 Re: reading text files!
Following on from what BigEvil said, you only need something like the following...
char buffer[1024];
myList.get(buffer, 1024, '=');
myList.get(); // skip '='
string key = trim(buffer);
myList.get(buffer, 1024, ';');
myList.get(); // skip ';'
string value = trim(buffer);
...where trim() could be based on string's find_first_not_of() and find_last_not_of()
- Bryan
# 3 Re: reading text files!
what can i do to ignore the commas which is within HAPPYELEPHANT? I would want it to abstract HAPPYELEPHANT(10,10,10) instead of
HAPPYELEPHANT(10 //break 10 //break 10)
statement to abstract:
"mr john", 42, 8, "likes banana", true, HAPPYELEPHANT(10,10,10)
# 4 Re: reading text files!
bmoodie, his data is not in "Name = mr john;" form, he just wants to retrieve it and store it in this way.
statement to abstract:
"mr john", 42, 8, "likes banana", true, HAPPYELEPHANT(10,10,10)
Is there any more data after happy elephant? Like I said earlier, you just have to use '\n' as the delim when reading the last field - MyList.Getline(buffer, 1024)
# 5 Re: reading text files!
Hey Norphos. Your problem is parsing your data. The best way to solve your problem is to define your expectation of the data you get.
For example, if you always expect the commas in HAPPYELEPHANT(10,10,10) or simmilar strings to be within '(' and ')', then it may be easiest to check for these and accept the commas in between. On the other hand, if you expect something like HAPPYELEPHANT(10,10,10) after the 5th comma, then count the commas and keep the 6th and 7th commas. There are many ways to set up a string parser to do this for you but the complexity of the parser depends on the complexity of the expected data sequence; you may not be able do do all of this when you grab the data from a file, some extra processing may be required.
The decision you have to make is how to structure your program to deal with your expected data in the simplest way. If the the data does not have any structure then you cannot expect to organize it.
# 6 Re: reading text files!
yes, the data does has a fixed structure. heres the exact statement:
"Pothios",0, PLAYER, HUMAN, 500, 200, 200.0,200,200, -1, 10,2000,1,true, 300.0, 50.0, CVECTOR(6542.0, 0.0 ,1298.0), CVECTOR(1.0, 0.0, 0.0), false
# 7 Re: reading text files!
Any reason for not storing it in binary files?!