Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

MFC: CStringArray and CObArray to store parsed text file - best way?

  • 21-01-2005 12:12PM
    #1
    Registered Users, Registered Users 2 Posts: 6,652 ✭✭✭


    I am trying to port TrainSched Palm app to Windows.
    The data file is a tab separated file. I am thinking of storing it in an array of arrays (CObArray contains pointers to multiple CStringArrays).
    First few lines of file:
    Station1 Station2 Station3
    Mon-Fri Sat Sun
    4 12 33 35 40 45 <- lines where different days/directions start.
    6:50 7:00 7:12
    8:00 8:10 8:22
    etc

    Using CTokenizer from CodeProject.com splitting each line to populate CStringArray is easy.
    I've experimented a little with two lines and adding two CStringArray to CObArray and it worked okay.
    I want to parse the text file when the file is loaded and then simply access the arrays as required, instead of parsing the text each time an action in the app requires it.

    Questions: Is this (CObArray/CStringArray) a good way to go? Are there better ways?

    When I ported by BusSched Palm app to Windows/Pocket PC I simply read in the entire data file, converted \n to NULLs while I maintained an array of LPSTR (char pointers) to the start of each line. I was only (re-)parsing one line at a time, whereas for TrainSched I'd be parsing multiple lines each time so I'd like to have a more structured approach.

    Current small experimental code:
    CObArray aAllTimes;
    	CString sTimeLine = "05:50	06:14	06:20	----	06:27	06:33	06:39	06:44	----";
    	CString sTime;
    	int all, i;
    
    
    	CStringArray *saSplitTimes = new CStringArray;
    	all = 0;
    	//saSplitTimes.SetSize( 0, 1 );
    	CTokenizer *tok = new CTokenizer( sTimeLine, _T( "	" ) );
    	i = 0;
    	while ( tok->Next( sTime ) )
    	{
    		//AfxMessageBox( sTime, MB_OK );
    		saSplitTimes->SetAtGrow( i, sTime );
    		i++;
    	}
    	aAllTimes.SetAtGrow( all, saSplitTimes );
    	all++;
    
    	//delete saSplitTimes;
    	saSplitTimes = new CStringArray;
    	sTimeLine = "06:50	07:14	07:20	----	07:27	07:33	07:39	07:44	----";
    	delete tok;
    	tok = new CTokenizer( sTimeLine, _T( "	" ) );
    	i = 0;
    	while ( tok->Next( sTime ) )
    	{
    		//AfxMessageBox( sTime, MB_OK );
    		saSplitTimes->SetAtGrow( i, sTime );
    		i++;
    	}
    	aAllTimes.SetAtGrow( all, saSplitTimes );
    	//delete saSplitTimes;
    
            // Display one element to prove it all worked.
    	saSplitTimes = (CStringArray*) aAllTimes.GetAt( 1 );
    	AfxMessageBox( saSplitTimes->GetAt( 1 ) );
    


Advertisement