调用另外一个exe文件时时这样写的char pass[512] = "C:\\My Documents\\Release\\TEST.exe test1 test2" test1和test2是两个参数 我用AfxGetApp()->m_lpCmdLine得到这些参数,但我怎么把它们分割开分别赋给两个变量呢,我刚学c++,望大家能够帮帮我!帖出代码。

解决方案 »

  1.   

    CString strcmdline=AfxGetApp()->m_lpCmdLine;
    CString strtest2;
    strtest2=strcmdline.Right(strcmdline.GetCount()-strcmdline.ReverseFind(' ')-1);  //得到test2参数
    strcmdline=strcmdline.Left(strcmdline.ReverseFind(' '));CString strtest1;
    strtest1=strcmdline.Right(strcmdline.GetCount()-strcmdline.ReverseFind(' ')-1);  //得到test1参数
      

  2.   

    可以用strtok 以函授
    也可以用CString 中的left,right,sub等。
      

  3.   

    strtok 怎么用,帮忙贴出代码啊?
      

  4.   

    查查CSDN
    Example
    /* STRTOK.C: In this program, a loop uses strtok
     * to print all the tokens (separated by commas
     * or blanks) in the string named "string".
     */#include <string.h>
    #include <stdio.h>char string[] = "A string\tof ,,tokens\nand some  more tokens";
    char seps[]   = " ,\t\n";
    char *token;void main( void )
    {
       printf( "%s\n\nTokens:\n", 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 );
       }
    }Output
    A string   of ,,tokens
    and some  more tokensTokens:
     A
     string
     of
     tokens
     and
     some
     more
     tokens
      

  5.   

    上面标红的地方编译出错啊error C2039: 'GetCount' : 'CString' 成员不存在
      

  6.   

    呵呵,打错字了,应该是GetLength(),不是GetCount().
      

  7.   

    怎么会看不懂呢?#include <string.h> 
    #include <stdio.h> char string[] = "C:\\My Documents\\Release\\TEST.exe test1 test2"; 
    char seps[]  = " :\\. "; //以冒号:、 \\ . 和空格为分格符,在你那里你可以用
    char *token; void main( void ) 

        token = strtok( string, seps ); 
      while( token != NULL ) 
      { 
                token = strtok( NULL, seps ); 把分割后的字符串存放在token 数组中
      } 
      

  8.   

    你不懂用token 的话
    你还可以用CString 中的left,right,sub等函数参照MSDN
      

  9.   

    strtok很好用啦,看一下MSND就知道了
      

  10.   

    __argv[]表示各个参数,__argc表示参数个数,程序本身是第0个参数。当命令行是:
    "C:\My Documents\Release\TEST.exe" test1 test2
    时,__argc的值是3,__argv[1]是"test1",__argv[2]是"test2"。
      

  11.   

    最后我这样写的int args;
    LPWSTR *szArglist;
    szArglist  = CommandLineToArgvW(GetCommandLineW(), &args); 
    CString params[6];
        for(int i = 0; i < args;i++){
            params[i] = szArglist[i];
    }