|
|
#1 (permalink) |
|
Monster Member
Join Date: Nov 2006
Location: Merrylands 2160
Age: 21
Posts: 3,687
Rep Power: 6
|
I have a text file with content like the following.
Code:
[0,7,19,3,1092][1,5,16,4,1064][2,15,14,3,1740][3,4,5,3,1048][4,7,11,2,1278] Lets say i declare 5 variables, x1 x2 x3 x4 x5. I want 0 to go to x1, 7 to x2, 19 to x3, 3 to x4 and 1092 to x5. I want to then complete a set of tasks on these variables. Once that task is complete i will then read in the next block [1,5,16,4,1064] over these variables. Any ideas? |
|
|
|
|
|
|
|
#2 (permalink) |
|
Contributing Member
Join Date: Jun 2006
Posts: 607
Rep Power: 3
|
using file functions, ifstream etc etc etc.
and then create a simple parser that relies on state. if [ then we create a new collection and append it to our total collections, if its a number, add it to the current collection etc etc.
__________________
92% of teens have moved on to rap. If you are part of the 8% who still listen to real music, copy and paste this into your signature. |
|
|
|
|
|
#3 (permalink) |
|
Monster Member
Join Date: Nov 2006
Location: Merrylands 2160
Age: 21
Posts: 3,687
Rep Power: 6
|
So like
ill read in 1 char and if the first char is the opening [ i will have a second statement within that loop or what ever to read in x(the value) with the , as a delimiter until it reads the ] then break the loop and repeat until EOF |
|
|
|
|
|
#5 (permalink) | |
|
Pro Member
Join Date: May 2006
Location: Sydney, Australia
Posts: 6,539
Rep Power: 9
|
Quote:
streaming the file in and parsing it on the fly would be a lot more efficient than loading the entire contents of the file into memory (could be 100's mbs) and performing string operations on it, considering each block of data in the file is only temporary/stateless and has no bearing on anything else, so there's no reason to have it all available at any one time. but anyway, khaless = 3rd year HIGH honour roll IT uni student, so yeah, he's a smart cookie, good kid, tries hard
__________________
OM NOM NOM |
|
|
|
|
|
|
#6 (permalink) | |
|
Contributing Member
Join Date: Jun 2006
Posts: 607
Rep Power: 3
|
Quote:
Yes, the reason I did not say any string functions is, what if the data file is very large, and with no new lines etc to buffered read on?
__________________
92% of teens have moved on to rap. If you are part of the 8% who still listen to real music, copy and paste this into your signature. |
|
|
|
|
|
|
#7 (permalink) | |
|
Pro Member
Join Date: May 2006
Location: Sydney, Australia
Posts: 6,539
Rep Power: 9
|
Quote:
__________________
OM NOM NOM |
|
|
|
|
|
|
#8 (permalink) |
|
Monster Member
Join Date: Jan 2007
Location: Brisbane, Australia
Age: 19
Posts: 3,500
Rep Power: 6
|
Woops I got this confused with C#.... Think it was when I read the "C# HALP" link which is the previous thread... heh
String.Split may still work... I don't know crap about C++
__________________
Jayson |
|
|
|
|
|
#9 (permalink) |
|
Monster Member
Join Date: Nov 2006
Location: Merrylands 2160
Age: 21
Posts: 3,687
Rep Power: 6
|
OK, my solution is this.
This Tokenizer function was found on the net. Code:
void Tokenize(const string& str,
vector<string>& tokens,
const string& delimiters = " ")
{
string::size_type lastPos = str.find_first_not_of(delimiters, 0);
string::size_type pos = str.find_first_of(delimiters, lastPos);
while (string::npos != pos || string::npos != lastPos)
{
tokens.push_back(str.substr(lastPos, pos - lastPos));
lastPos = str.find_first_not_of(delimiters, pos);
pos = str.find_first_of(delimiters, lastPos);
}
}
Code:
do{
getline(inputFile,str,'[');
Tokenize(str, tokens,",");
}while(!inputFile.eof());
I then incorporated this piece of code to store the data from the text file into an array of objects(i was going to use an vector of objects but the hell with that). Code:
for (unsigned int i = 0; i < tokens.size() ;i=i+5)
{
temp=tokens[i+4];
temp=temp.substr(0, temp.length() - 1);
objOrder[x].orderId = atoi(tokens[i].c_str());
objOrder[x].PCid = atoi(tokens[i+1].c_str());
objOrder[x].quantity = atoi(tokens[i+2].c_str());
objOrder[x].dueDate = atoi(tokens[i+3].c_str());
objOrder[x].profit = atoi(temp.c_str());
/**** Reference Block ****/
/* OrderStatus 0 = First */
/* OrderStatus 1 = Delay */
/* OrderStatus 2 = Cancel*/
objOrder[x].totalProfit = objOrder[x].profit * objOrder[x].quantity - (objProduct.pccycle(objOrder[x].PCid)*100*objOrder[x].quantity);
if(objOrder[x].totalProfit<=0){
objOrder[x].orderStatus = 1;
}else{
objOrder[x].orderStatus = 0;
}
x++;
}
Thanks for the help. |
|
|
|
|
|
#12 (permalink) |
|
Pro Member
Join Date: Jun 2006
Age: 19
Posts: 5,593
Rep Power: 8
|
Just thought I would add.. that there is in fact strtok() which does pretty much what that internet version does in STL.
chem
__________________
There are no stupid questions... but there are alot of inquisitive idiots. - |
|
|
|




)






