When I run this code, the variable s has MZ as the first 2 characters, I never put them there, I don't know how they got there. I think it might have something to do with strcat, does strcat modify the source paramater at all?

void Con_Draw()
{
	int i;
	char *s;

	s = malloc(256*CON_LINES);
	s[0]=0;
	for(i=CON_LINES ; i>0 ; i--)
		strcat(s,con_lines[i]);

	Draw_String(-3,-1.5,s);

	free(s);
}

Recommended Answers

All 15 Replies

>does strcat modify the source paramater at all?
No, it's a pointer to const char. This strikes me more as a fencepost error with your loop index, where it starts at CON_LINES rather than CON_LINES-1.

Thanks, that fixed it :)

Okay, now I have another problem. In this code, after the first call to strtok, the variable becomes "developer" when it was originally "developer;developer 0;developer". I would have expected it to become "developer 0;developer" after the first call..

void Con_Execute(char *text)
{
	char *pch;
	char string[10240];

	strcpy(string,text);

	if(!strstr(string,";"))
	{
		Cmd_ExecuteString(string);
		return;
	}

	pch = strtok(string,";");
	while(pch)
	{
		Cmd_ExecuteString(string);
		pch = strtok(NULL,";");
	}
}

Three things:

  1. You call Cmd_ExecuteString(string) in the tokenization loop. Shouldn't it be Cmd_ExecuteString(pch)?
  2. Which variable are we talking about here? I'm assuming string , because it coincides with your description of the problem.
  3. strtok modifies the source string by adding null characters at the separator. This is how you get a nice happy token as the return value without any dynamic allocation. If you start with "developer;developer 0;developer" and change it to "developer\0developer 0;developer", any operation on a string (including printing) will stop at the null character.

Okay, so how do I get it to do what I want? And yes I'm talking about string.

What do you want?

for pch to equal the next token in sting after each call to strtokwhen they're separated by ';'

Well I dont think strtok will give you that
What you can do is something like this

Use strtok to find the next token
Use strlen to find the length of this token. Let this be l1
Now your string of interest is from l1 to end of the original string

Not to interrupt your focus on strtok, but you could "roll your own" on such a simple task, in half the time, and half the trouble.

start at the first char of the string
in a loop, "walk" through the string, until you find a comma, assigning a
small char buffer, the value of each letter as you walk the string.

When you reach the comma, set it's index number to an offset variable, and put
an end of string char onto your small char buffer, if it didn't have one already.

Now move each letter in the string array, down by the offset variable value, within the string. I call it "boogie left". ;)

If the comma was at index 15, then string[i-offset] = string, in a loop.

And you're ready to process your small char buffer, and repeat this, in your loop.

It's harder to describe it than it is to code it. For small and moderate strings, it's plenty fast, if not the most efficient answer.

I don't understand why I would need to do that when strtok has worked the way that I thought it would every other time I have used it in this project and others :S

Maybe it's the zero at the end of one of the words, maybe you don't understand strtok() as well as you thought, maybe it's something entirely simple and different.

Maybe tomorrow or next month, you'll slap your head and say "Oh, of course!". Who knows.

When you are stuck with something that's not working - and you've done everything you can think of - it's time to do it another way. There's nothing magical about strtok().

I just really don't understand why it's only getting one token because the same method works here and it gets more than one token here:

void Cmd_TokenizeString(char *string)
{
    static char str[1024];
    char *pch;
    int i;

    for(i=0 ; i<MAX_ARGS ; i++)
        cmd_argv[i][0]=0;

    strcpy(cmd_args,string);
    strcpy(str, string);

    pch = strtok(str, " ");
    i=0;
    while(pch)
    {
        strcpy(cmd_argv[i],pch);
        pch = strtok(NULL, " ");
        i++;
    }

    cmd_argc = i;
}

> for pch to equal the next token in sting after each call to strtokwhen they're separated by ';'
pch does equal the next token in the string after each call to strtok. But you're processing the string variable, not the pch variable. I already mentioned this, you're calling Cmd_ExecuteString(string) in the tokenization loop, not Cmd_ExecuteString(pch) .

i dont really understand whats going on here [the question itself].. so I am adding myself, So if i had missed something i will get it by the time it gets solved

@OP or Narue , one doubt
whats the function Cmd_ExecuteString() doing? I was not able to make out from the question.. Is it any standard library function?

>whats the function Cmd_ExecuteString() doing?
That's largely irrelevant, I think. Pretend that it's a placeholder for any function accepting a string argument. Hell, you can pretend that it's a macro for puts:

#define Cmd_ExecuteString puts
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.