Thread: strtok problem again

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    61

    strtok problem again

    hello, i have the following code... in order to resolve strtok problem, i made a duplicate copy of cmdList named cmdListDup and did a strtok on cmdListDup instead... why am i still getting a modified cmdList after the last loop? :/

    Code:
    /* concatenate command and get command count */
    	for(i = 0; i < cmdWords; i++) {
    		strcat(cmdStr, words[i]);
    		strcat(cmdStr, " ");
    		if(strcmp(words[i], "|") == 0 ) {
    			cmdCount ++;
    		}
    	}
    	/* Remove | */
    	temp = strtok(cmdStr, "|");
    	i = 0;
    	while(temp) {
    		cmdList[i] = temp;         
    		cmdListDup[i] = temp; /* duplicate of cmdlist */
    		temp = strtok(NULL, "|");
    		i++;
    	}
    	
    	printf("cmd b4 forloop:&#37;s\n", cmdList[0]);  //prints cmd b4 forloop: hello -a
    	/* get command for each commands */
    	for(i = 0; i <= cmdCount; i++) {
    		temp = strtok(cmdListDup[i], " ");
    		cmd[i] = temp;
    		printf("cmd:%s\n", cmd[i]);
    	}
           printf("cmd after forloop:%s\n", cmdList[0]); //prints cmd afer forloop: hello
    Last edited by cstudent; 04-24-2008 at 12:24 AM.

  2. #2
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by cstudent View Post
    hello, i have the following code... in order to resolve strtok problem, i made a duplicate copy of cmdList named cmdListDup and did a strtok on cmdListDup instead... why am i still getting a modified cmdList after the last loop? :/

    Code:
    /* concatenate command and get command count */
    	for(i = 0; i < cmdWords; i++) {
    		strcat(cmdStr, words[i]);
    		strcat(cmdStr, " ");
    		if(strcmp(words[i], "|") == 0 ) {
    			cmdCount ++;
    		}
    	}
    	/* Remove => */
    	temp = strtok(cmdStr, "|");
    	i = 0;
    	while(temp) {
    		cmdList[i] = temp;
    		cmdListDup[i] = temp;
    		temp = strtok(NULL, "|");
    		i++;
    	}
    	
    	printf("cmd b4 forloop:%s\n", cmdList[0]);  //prints cmd b4 forloop: hello -a
    	/* get command for each commands */
    	for(i = 0; i <= cmdCount; i++) {
    		temp = strtok(cmdListDup[i], " ");
    		cmd[i] = temp;
    		printf("cmd:%s\n", cmd[i]);
    	}
           printf("cmd after forloop:%s\n", cmdList[0]); //prints cmd afer forloop: hello
    A better questing is, why are you talking about using strtok on cmdList, when you're using it on cmdStr? Also, you're not assiging cmdList[i] to temp, you are assigning temp to cmdList[i] - e.g. the opposite.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    61
    Quote Originally Posted by IceDane View Post
    A better questing is, why are you talking about using strtok on cmdList, when you're using it on cmdStr? Also, you're not assiging cmdList[i] to temp, you are assigning temp to cmdList[i] - e.g. the opposite.
    why am i assigning the values to temp instead? i dun understand

  4. #4
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by cstudent View Post
    why am i assigning the values to temp instead? i dun understand
    Nevermind what I said, I misunderstood the code, mainly because you didn't post the whole code.

    Post the whole code and we'll be able to help you.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    61
    i edited the code to the following and its working.

    Code:
    /* concatenate command and get command count */
    	for(i = 0; i < cmdWords; i++) {
    		strcat(cmdStr, words[i]);
    		strcat(cmdStr, " ");
    		if(strcmp(words[i], "|") == 0 ) {
    			cmdCount ++;
    		}
    	}
    	/* Remove => */
    	temp = strtok(cmdStr, "|");
    	i = 0;
    	while(temp) { 
                    cmdList[i] = (char*)malloc(1000);           /* allocate memory */
                    cmdListDup[i] = (char*)malloc(1000);    /* allocate memory */
    		strcpy(cmdList[i], temp);
                    strcpy(cmdListDup[i], temp);
    		temp = strtok(NULL, "|");
    		i++;
    	}
    	
    	printf("cmd b4 forloop:&#37;s\n", cmdList[0]);  
    	/* get command for each commands */
    	for(i = 0; i <= cmdCount; i++) {
    		temp = strtok(cmdListDup[i], " ");
    		cmd[i] = temp;
    		printf("cmd:%s\n", cmd[i]);
    	}
           printf("cmd after forloop:%s\n", cmdList[0]);

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    There is something dubious about that while(temp) loop to me.... You may want to rethink your memory allocation strategy a bit... Things like that usually have a lot of room for memory leaks and just general system resource draining. But, whatever tickles your pickle bud.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Or try my own strtok instead of making duplicates. It's up to you, but I find it far better than having to worry about freeing allocated memory.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. Replies: 6
    Last Post: 04-28-2006, 12:06 PM
  3. Strange problem - strtok
    By AngKar in forum C Programming
    Replies: 7
    Last Post: 04-23-2006, 07:36 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  5. Replies: 5
    Last Post: 11-07-2005, 11:34 PM