Advertisement

tokens?

Started by August 21, 2000 09:21 PM
6 comments, last by InFerN0 24 years, 4 months ago
whats the best way to construct a system for parsing a string for commands and arguments. I haven''t been able to find anything on the subject so any help would be appreciated. Thanks!
InFerN0Not all who wander are lost...
the only thing you really need to know is how to utilize the strtok() function. once you learn how to use it, you''ll be parsing command data in no time... if you need more info, e-mail me...

david

--
davidw@heehaw.com
neonstar entertainment
--david@neonstar.netneonstar entertainment
Advertisement
Decide on your delimiters... usually you would delimit based on spaces, so you go through, find the first space, grab everything before that, store it away, repeat... you can then process each word individually. As mentioned, strtok is good if you like old style char* strings, or you can do something similar and probably more readable with the std::string too.
I been working on this. Any Idea why the following code crashes?
    #include <stdio.h>#include <string.h>#include <conio.h>//this function gets inputchar *GetInput(char *buffer){int c=0, index=0;// loop while user hasn''t hit returnwhile((c=getch())!=13)     {// implement backspace     if (c==8 && index>0)        {        buffer[--index] = '' '';        printf("%c %c",8,8);        } // end if backspace     else     if (c>=32 && c<=122)        {        buffer[index++] = c;        printf("%c",c);        } // end if in printable range     } // end whilebuffer[index] = 0;if (strlen(buffer)==0)   return(NULL);elsereturn(buffer);} // end Get_Linechar *string;char seps[]   = " ,\t\n";char *token;void main( void ){   	GetInput(string);		/* Establish string and get the first token: */   token = strtok( string, seps );         while( token != NULL )    {      /* While there are tokens in "string" */      	   printf( " %s\n", token );      /* Get next token: */     	   token = strtok( NULL, seps );   	}}    

thanks

InFerN0Not all who wander are lost...
Ok, here's the code I think is suspect (I haven't got a compiler
handy so I could be wrong). It's the bit just after your input
function.

char *string;
char seps[] = " ,\t\n";
char *token;

void main( void ){
GetInput(string);
.....

Now you've defined 'string' as char *, but you haven't allocated
any memory to it. You could either define 'string' as an
array (ie: char string[255]; ), or modify your main() like this.

void main( void) {
string = new char[255]; //or whatever size you want there
GetInput(string);
.....

At least that's how it looks to me.


----------
Disco Love For Everyone

Edited by - SpazBoy_the_Mitey on August 22, 2000 10:46:50 PM
----------"i think that all this talking and such is paining my head to astounding annoyance" - Erick"Quoting people in your tag is cool. Quoting yourself is even cooler" - SpazBoy_the_MiteyDisco Love For Everyone
Just tested it out in MSVC 6 and you were right. Good call

Anthracks
Advertisement
quote: Just tested it out in MSVC 6 and you were right. Good call


*phew*
(spazboy feeling pretty gratified right about now)


----------
Disco Love For Everyone
----------"i think that all this talking and such is paining my head to astounding annoyance" - Erick"Quoting people in your tag is cool. Quoting yourself is even cooler" - SpazBoy_the_MiteyDisco Love For Everyone
Thanks Guys.
InFerN0Not all who wander are lost...

This topic is closed to new replies.

Advertisement