如果有CString str="12 23 56 45..."(或者是char*)
如何从中取出整数12,23,56,45...付值到一个整型数组中

解决方案 »

  1.   

    CString::GetBuffer()能够获得一个char*指向内部的字符串缓冲区,用完以后再用CString::ReleaseBuffer()释放即可
      

  2.   

    int a[100];
    int i=0;
    while(str.getlength>0)这个条件不一定完全正确哦
    {
    str1=str.left(str.find(" "));
    str=str.right(str.getlength()-str.find(" ")-1);
    a[i]=atoi(str1);
    i++;
    }
      

  3.   

    #include<iostream>
    #include<strstream>
    using namespace std;
    int main(int argc,char* argv[])
    { char sz[]="111 222 3  44 55 666";
    istrstream  is( sz );
    int x=0;
    while(is>>x){
    cout<<x<<"\n";
    }
    return 0;
    }
      

  4.   

    CString::Tokenize()可以拆分字符串。
      

  5.   

    akirya(坏[其实偶不是什么所谓的坏人]) cool ~-~
      

  6.   


    int foo( int buf[], int size, char str[] )
    {
    int cnt = 0;
    if( cnt >= size )
    return cnt;
    buf[0] = 0;
    while( *str++ )
    {if( *str >= '0' && *str <= '9' )
    {
    buf[cnt] = buf[cnt] * 10 + *str - '0';
    }
    else if( cnt < size - 1 )
    cnt ++;
    }
    return cnt;
    }
      

  7.   

    http://www.wanrenkeng.com/clib/string/strtok.html
    试试strtok
      

  8.   

    不是每个数字后面有个空格嘛,以它作为分隔符查找啊
    CString::Find,CString::Left
      

  9.   

    直接用strtok得到字符串token序列,再转成int.
    char* token;
    char seps[]=" ";
    int data[100];
    int count=0;
    token=strtok(str,seps);
    data[count++]=atoi(token);
    while(token!=NULL)
    {
    data[count++]=atoi(token);
    token=strtok(NULL,seps);
    }
      

  10.   

    不是每个数字后面有个空格嘛,以它作为分隔符查找啊
    CString::Find,CString::Left
    -------------------------------------
    我先去试试, 没使过
      

  11.   

    // crt_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;int main( void )
    {
       printf( "Tokens:\n" );
       /* 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
    Tokens:
     A
     string
     of
     tokens
     and
     some
     more
     tokens
      

  12.   

    补上串流的代码: 
    #include <iostream>
    #include <strstream>
    using namespace std;int main(void)
    {
    char *str = "12 23       45";
    int arr[3];
    int j = 0;
    int temp = 0;
    istrstream in(str);
    for(j = 0; j < 3; j++)
    {
    in >> temp;
    arr[j] = temp;
    } for(j = 0; j < 3; j++)
    {
    cout << arr[j] << "\t";
    }
    cout << endl;
    return 0;
    }
      

  13.   

    写了个更加通用的,并附加测试#include <stdio.h>
    #include <stdlib.h>char* trim_num( int *buf, char str[] )
    {
    int started = 0;

    *buf = 0; while( 1 )
    { if( *str >= '0' && *str <= '9' )
    {
    if( started == 0 )
    {
    *buf = *str - '0';
    started = 1;
    }
    else
    {
    *buf = *buf * 10 + *str - '0';
    }

    }
    else if( *str == '\0' )
    {
    if( started == 0 )
    {
    return NULL;
    }
    else
    {
    return str;
    }
    }
    else
    {
    if( started != 0 )
    {
    return str;
    }

    }

    str ++;

    }    return str;
        
    }char *str = "12 1441,42,,i4432  424";int main( int argc, char *argv[] )
    {
    int num;
    char* cur = str;

    while( cur = trim_num( &num, cur ) )
    {
    printf( "%d\n", num );
    }

    system( "PAUSE" );
    return (0);
    }
    输出:
    12
    1441
    42
    4432
    424
    请按任意键继续 . . .
      

  14.   

    任意数量的非数字的字符都可以被解析为分隔符,并且,函数返回字符串当前被解析到的位置
    如果到达字符串尾则返回NULL
      

  15.   

    sscanf(data,"%d %d %d %d %d %d %d %d",&m1,&m2,&m3,&m4,&m5,&m6,&m7,&m8);
    data是char *型的`