Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

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

Options
  • 21-01-2005 12:12pm
    #1
    Registered Users Posts: 6,508 ✭✭✭


    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