Old 15-05-2008   #1 (permalink)
Monster Member
 
SargeRX8's Avatar
 
Join Date: Nov 2006
Location: Merrylands 2160
Age: 21
Posts: 3,687
Rep Power: 6
Default C++ Reading in text file

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]
How do i go about reading in each block? For example we have [0,7,19,3,1092]

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?
__________________

Click the sig to watch my Rambo Video

HuggyCHEA[2f][2sxc]: im a gentleman gee
HuggyCHEA[2f][2sxc]: but get me drunk broo, ill grab your tits
SargeRX8 is offline   Reply With Quote
Old 15-05-2008   #2 (permalink)
Contributing Member
 
Join Date: Jun 2006
Posts: 607
Rep Power: 3
Default

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.
Khaless is offline   Reply With Quote
Old 15-05-2008   #3 (permalink)
Monster Member
 
SargeRX8's Avatar
 
Join Date: Nov 2006
Location: Merrylands 2160
Age: 21
Posts: 3,687
Rep Power: 6
Default

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
__________________

Click the sig to watch my Rambo Video

HuggyCHEA[2f][2sxc]: im a gentleman gee
HuggyCHEA[2f][2sxc]: but get me drunk broo, ill grab your tits
SargeRX8 is offline   Reply With Quote
Old 16-05-2008   #4 (permalink)
Monster Member
 
Join Date: Jan 2007
Location: Brisbane, Australia
Age: 19
Posts: 3,500
Rep Power: 6
Default

Or you can use functions like String.Split()

Pressing F1 is the best way to find out about how it works (Assuming you're using Visual Studio )
__________________
Jayson
Jayso is offline   Reply With Quote
Old 16-05-2008   #5 (permalink)
Pro Member
 
nuk3's Avatar
 
Join Date: May 2006
Location: Sydney, Australia
Posts: 6,539
Rep Power: 9
Default

Quote:
Originally Posted by Jayso View Post
Or you can use functions like String.Split()

Pressing F1 is the best way to find out about how it works (Assuming you're using Visual Studio )
I know nothing about c++, but I'll just make a generalised probably unfounded observational pointless comment:

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
nuk3 is offline   Reply With Quote
Old 16-05-2008   #6 (permalink)
Contributing Member
 
Join Date: Jun 2006
Posts: 607
Rep Power: 3
Default

Quote:
Originally Posted by nuk3 View Post
I know nothing about c++, but I'll just make a generalised probably unfounded observational pointless comment:

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
How did you know that? I hope it was Tim, and you were not stalking me... I'm Honours Now (4th year)

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.
Khaless is offline   Reply With Quote
Old 16-05-2008   #7 (permalink)
Pro Member
 
nuk3's Avatar
 
Join Date: May 2006
Location: Sydney, Australia
Posts: 6,539
Rep Power: 9
Default

Quote:
Originally Posted by Khaless View Post
How did you know that? I hope it was Tim, and you were not stalking me... I'm Honours Now (4th year)

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?
haha yeah, tim told me, but if it makes you feel any better I've been tapping into your interwebs and stoling your megahurtz.

__________________
OM NOM NOM
nuk3 is offline   Reply With Quote
Old 16-05-2008   #8 (permalink)
Monster Member
 
Join Date: Jan 2007
Location: Brisbane, Australia
Age: 19
Posts: 3,500
Rep Power: 6
Default

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
Jayso is offline   Reply With Quote
Old 19-05-2008   #9 (permalink)
Monster Member
 
SargeRX8's Avatar
 
Join Date: Nov 2006
Location: Merrylands 2160
Age: 21
Posts: 3,687
Rep Power: 6
Default

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);
    }
}
I used this along with this piece of code

Code:
do{
        getline(inputFile,str,'[');
        Tokenize(str, tokens,",");

    }while(!inputFile.eof());
The '[' skips that character in when reading each block. The tokenizer reads in each value up to the ,.

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++;
    }
I cheated abit in getting rid of the ] at the end of the block. What i done was read in the value after the comma, say for example the end of the block is ..,9]. What i done was read in the 9] then use a string function(in bold) to remove -1 from the end of the string...seems to work just fine

Thanks for the help.
__________________

Click the sig to watch my Rambo Video

HuggyCHEA[2f][2sxc]: im a gentleman gee
HuggyCHEA[2f][2sxc]: but get me drunk broo, ill grab your tits
SargeRX8 is offline   Reply With Quote
Old 19-05-2008   #10 (permalink)
Monster Member
 
shirasE_'s Avatar
 
Join Date: Jan 2007
Location: Melbourne, Australia.
Age: 18
Posts: 3,555
Rep Power: 5
Send a message via ICQ to shirasE_ Send a message via AIM to shirasE_ Send a message via MSN to shirasE_ Send a message via Yahoo to shirasE_ Send a message via Skype™ to shirasE_
Default

tl;dr

split function.
__________________
AMD 5000+BE@3.21GHZ//2GB OCZREAPER//ATI HD3870@850/1301
ANTEC 900//RAZER TARANTULA//TT TAI-CHI M2//SS 5HV2 XFI
#GotGames #MelbCSS #s2.au #Reality.Gaming
#TRAiNWRECK melb.lan.net.invite - kto_-[1b]
shirasE_ is offline   Reply With Quote
Old 19-05-2008   #11 (permalink)
Monster Member
 
SargeRX8's Avatar
 
Join Date: Nov 2006
Location: Merrylands 2160
Age: 21
Posts: 3,687
Rep Power: 6
Default

Quote:
Originally Posted by shirasE_ View Post
tl;dr

split function.
Theres no standard split function in C++, but the code i used sorts that issue out.
__________________

Click the sig to watch my Rambo Video

HuggyCHEA[2f][2sxc]: im a gentleman gee
HuggyCHEA[2f][2sxc]: but get me drunk broo, ill grab your tits
SargeRX8 is offline   Reply With Quote
Old 17-06-2008   #12 (permalink)
Pro Member
 
chemicalNova's Avatar
 
Join Date: Jun 2006
Age: 19
Posts: 5,593
Rep Power: 8
Default

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.
-
chemicalNova is offline   Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT +10. The time now is 08:42 AM.


Powered by vBulletin® Version 3.7.5
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0