Help with parsing block of text from file

Posted By: silencer

Help with parsing block of text from file - 04/06/09 21:50

So I have a text file I need to read from. In the text file the text is formatted in the following manner.

Code:
[someString1,someString2,someString3]
[someStrhing4,seomStrhing5,someStrhing6]


I need to write a loop that will recognize the "[" opening bracket, bypass it, and then continue reading the strings (using the "," as a delimit character) until the next "[" opening bracket is hit, at which point the loop should terminate.

Additionally I have an int variable counting the amount of "[" opening brackets hit while parsing. I will use this counter to switch between different blocks of strings when needed.

So far I have the following:
Code:
	fhandle = file_open_read (filename); // Opening file
	
	while(eof != -1){
		eof = file_str_read(fhandle, test_str);
		str_cpy( (dialog_file.pstring)[count], test_str); //Pushing lines into array
	   count += 1;
	}


How do I utilize an extra delimiter character to count the "[" opening brackets?
Posted By: EvilSOB

Re: Help with parsing block of text from file - 04/07/09 03:55

Try this (tested) code to see of its what you are after.
Code:
	var fhandle = file_open_read("diag.txt");
	var groups=0;
	while(eof != -1)
	{
		eof = file_str_read(fhandle, test_str);
		if(eof>0) 
		{
			if(str_to_asc(test_str)==str_to_asc(_str("[")))								// <<- can be simplified to if((test_str.chars)[0]==91)
			{	
				groups += 1;	//increment group counter
				str_clip(test_str,1);	//remove leading "["
			}
			if((test_str.chars)[str_len(test_str)-1]==str_to_asc(_str("]")))		// <<- can be simplified to if((test_str.chars)[str_len(test_str)-1]==93)
			{	str_trunc(test_str,1);	/*remove any "]"*/	}		
			str_cpy( (dialog_file.pstring)[count], test_str);		//Push lines into array
			count += 1;		//increment total-strings count
		}
	}




Posted By: silencer

Re: Help with parsing block of text from file - 04/07/09 16:57

Ah thanks EvilSOB, that seems to be what I'm looking for. I didn't know about the "str_clip" and str_trunc functions.

Just to check, if I wanted to say start pushing the strings from "block 2" in my text file, I'd just need an if-statement around the block where I'm pushing the strings into the array to check if the counter was equal to 2 (or 3 or 4, depending on what block of strings I wanted to put into the array).
Posted By: EvilSOB

Re: Help with parsing block of text from file - 04/08/09 00:46

just around the STR_CPY and the count would DO, but the
loop would keep reading after its gotten what it wants. Not efficient.
Put a break; inside that if too and its better.
© 2024 lite-C Forums