Trouble with strtok()

This is a discussion on Trouble with strtok() within the C Programming forums, part of the General Programming Boards category; I'm using this command-line parameters in my executable: myprogram.exe c:\programmi\driver.exe c:\programmi\seti.exe My code looks for the parameter , it is ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    161

    Trouble with strtok()

    I'm using this command-line parameters in my executable:

    myprogram.exe c:\programmi\driver.exe c:\programmi\seti.exe

    My code looks for the parameter, it is cut in 2 pieces divided by the "space" and I want to save the 2 pieces.. then every piece is cut in smaller pieces divided by "\" untill I get the executable name.. so at the end I want to print the 2 path i gave as parameter and the name of the 2 exe found in the path given :

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <windows.h>
    
    
      int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, char * lpCmdLine, int  nCmdShow)
    
     {
    
      char separatoriexe[] = "\\";
      char separatori[] = " ";
      char *seti;
      char *tempseti;
      char *tempdriver;
      char *setiexe;
      char *driver;
      char *driverexe;
    
      driver = strtok( lpCmdLine, separatori );
      seti = strtok( NULL, separatori );
          
      tempseti = strtok( seti, separatoriexe );
        while( tempseti != NULL )
       {
          setiexe = tempseti;
          tempseti = strtok( NULL, separatoriexe );
          
       }
       
      tempdriver = strtok( driver, separatoriexe );
        while( tempdriver != NULL )
       {
          driverexe = tempdriver;
          tempdriver = strtok( NULL, separatoriexe );
          
       }
    
       printf( "Driver: %s\n", driver );
       printf( "Seti: %s\n", seti );   
       printf( "Exe di Seti: %s\n", setiexe );
       printf( "Exe di Driver: %s\n", driverexe );
       
        
       system("PAUSE");	
       return 0;
     }
    But it seems that the strtok modify the string after the manipulation...what should I do to conserve the strings (I need them in all the code so I HAVE to conserve them)??

    I get this wrong output:

    Driver: c:
    Seti: c:
    Exe di Seti: seti.exe
    Exe di Driver: driver.exe
    Instead this one that is right:

    Driver: c:\programmi\driver\driver.exe
    Seti: c:\programmi\driver\seti.exe
    Exe di Seti: seti.exe
    Exe di Driver: driver.exe
    help please
    Last edited by BianConiglio; 05-08-2004 at 06:38 PM.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    The problem is that strtok() replaces the delimiter with a null character. Since strings are null terminated it only display the first part.

    Look into strchr() and strncpy() for your solution.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    thanx very much for the help..

    i've added

    char setipath[MAX_PATH];
    char driverpath[MAX_PATH];

    ...
    ...
    ...

    strcpy (setipath, seti);
    strcpy (driverpath, driver);


    and it works
    Last edited by BianConiglio; 05-08-2004 at 07:08 PM.
    This forum is the best one I've ever seen. Great ppl, great coders

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 04:48 PM
  2. strtok is causing segmentation fault
    By yougene in forum C Programming
    Replies: 11
    Last Post: 03-08-2008, 09:32 AM
  3. trying to use strtok() function to parse CL
    By ohaqqi in forum C Programming
    Replies: 15
    Last Post: 07-01-2007, 09:38 PM
  4. Help debugging my program
    By shoobsie in forum C Programming
    Replies: 4
    Last Post: 07-05-2005, 07:14 AM
  5. strtok tokenizing on spaces as well as my delimiter
    By snowblind37 in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2004, 12:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21