CString and array.

Discussion in 'C++' started by Oskar, Jul 1, 2004.

  1. Oskar

    Oskar Guest

    Hi.
    I`m new in cpp and i have a litlle problem.
    i have a CString from Edit Box (eg."aaa bbb ccc 7327373 d feaf 323 dvjiv
    234") and i want to put the data (space separated) into an array.It shuld be
    somethign like this.

    array1[0]="aaa";
    array1[1]="bbb";
    ....
    array1[43653]="234";

    The thing is i don`t have any clue how to do it. :|
    Thx for help

    --

    Greetings
    Oskar
    Oskar, Jul 1, 2004
    #1
    1. Advertising

  2. Oskar

    JKop Guest

    Oskar posted:

    > Hi.
    > I`m new in cpp and i have a litlle problem.
    > i have a CString from Edit Box (eg."aaa bbb ccc 7327373 d feaf 323
    > dvjiv 234") and i want to put the data (space separated) into an
    > array.It shuld be somethign like this.
    >
    > array1[0]="aaa";
    > array1[1]="bbb";
    > ...
    > array1[43653]="234";
    >
    > The thing is i don`t have any clue how to do it. :|
    > Thx for help


    CString is OFF-TOPIC here.

    As for an array of strings:

    char* p_array_strings[50];

    p_array_strings[0] = "Aa";
    p_array_strings[1] = "Bb";
    p_array_strings[2] = "Cc";
    p_array_strings[3] = "Dd";
    p_array_strings[4] = "Ee";
    p_array_strings[5] = "Ff";


    -JKop
    JKop, Jul 1, 2004
    #2
    1. Advertising

  3. Oskar

    Oskar Guest

    U¿ytkownik "JKop" <> napisa³ w wiadomo¶ci
    news:HlUEc.3615$...
    > CString is OFF-TOPIC here.

    Dunno why....(?)
    >
    > As for an array of strings:
    >
    > char* p_array_strings[50];
    >
    > p_array_strings[0] = "Aa";
    > p_array_strings[1] = "Bb";
    > p_array_strings[2] = "Cc";
    > p_array_strings[3] = "Dd";
    > p_array_strings[4] = "Ee";
    > p_array_strings[5] = "Ff";
    >

    I know how to add data to array, but i need function or smth. that get that
    data from CString (separates by spaces) and add it do array.

    >
    > -JKop


    Greetz.
    Oskar, Jul 1, 2004
    #3
  4. Oskar wrote:
    > Hi.
    > I`m new in cpp and i have a litlle problem.
    > i have a CString from Edit Box (eg."aaa bbb ccc 7327373 d feaf 323 dvjiv
    > 234") and i want to put the data (space separated) into an array.It shuld be
    > somethign like this.
    >
    > array1[0]="aaa";
    > array1[1]="bbb";
    > ...
    > array1[43653]="234";
    >
    > The thing is i don`t have any clue how to do it. :|
    > Thx for help
    >


    The CString class is a Microsoft Specialty and best discussed
    in a Microsoft newsgroup.

    If it were an std::string, you could use the "find" method
    to look for spaces.

    std::string text;
    std::string string_array[43653];

    string::size_type position(0);
    string::size_type end_posn;

    end_posn = text.find(" ", posn);
    string_array[0] = text.substr(posn, end_posn - posn);
    posn = text.find_firs_not_of(" ", end_posn);
    end_posn = text.find(" ", posn);
    string_array[1] = text.substr(posn, end_posn - posn);

    and so on.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    Thomas Matthews, Jul 1, 2004
    #4
  5. Oskar

    Sharad Kala Guest

    "Oskar" <> wrote in message
    news:cc138h$4ru$...
    > Hi.
    > I`m new in cpp and i have a litlle problem.
    > i have a CString from Edit Box (eg."aaa bbb ccc 7327373 d feaf 323 dvjiv
    > 234") and i want to put the data (space separated) into an array.It shuld

    be
    > somethign like this.
    >
    > array1[0]="aaa";
    > array1[1]="bbb";
    > ...
    > array1[43653]="234";
    >
    > The thing is i don`t have any clue how to do it. :|
    > Thx for help
    >


    Some suggestions -
    1. CString is not a standard C++ class. Use std::string instead (#include
    <string>).

    string s="abcd efgh ...";

    2. Use stringstreams
    istringstream istr(s); // Initialize with your string

    3. Then don't use arrays, use std::vector class. It's a dynamically growing
    array.
    vector<string> vec;

    4. Extract strings from stringstream.
    string temp;
    istr >> temp; // temp now contains abcd
    vec.push_back(temp); // Push into the vector
    istr >> temp; // temp contains efgh
    so on...

    -Sharad
    Sharad Kala, Jul 1, 2004
    #5
  6. Oskar

    David Harmon Guest

    On Thu, 1 Jul 2004 15:33:20 +0200 in comp.lang.c++, "Oskar"
    <> wrote,
    >I know how to add data to array, but i need function or smth. that get that
    >data from CString (separates by spaces) and add it do array.


    CString is off topic here. For splitting a std::string, see my example
    at
    http://groups.google.com/groups?selm=

    See the welcome message posted twice per week in comp.lang.c++ or
    available at http://www.slack.net/~shiva/welcome.txt
    David Harmon, Jul 1, 2004
    #6
  7. Oskar

    Guest Guest

    strtok function does what you want.
    Guest, Jul 1, 2004
    #7
  8. "Oskar" <> wrote:

    > I`m new in cpp and i have a litlle problem.
    > i have a CString from Edit Box (eg."aaa bbb ccc 7327373 d feaf 323 dvjiv
    > 234") and i want to put the data (space separated) into an array.It shuld be
    > somethign like this.
    >
    > array1[0]="aaa";
    > array1[1]="bbb";
    > ...
    > array1[43653]="234";


    You haven't defined CString or array1.

    So I'll assume:

    typedef char CString[80];
    CString array1[5];

    You can use various tricks for separating-out
    space-separated "words". My favorite is a stringstream.
    Try something like this:

    #include <iostream>
    #include <cstring>
    #include <sstream>

    using std::istringstream;
    using std::cout;
    using std::endl;

    int main()
    {
    typedef char CString[80];
    CString array1[5];
    CString string1 = "my red dog is handsome";
    std::istringstream blat(string1);
    char buffer[65];
    for (int i=0 ; i<5; ++i)
    {
    blat >> buffer;
    if (!blat) break;
    strcpy(array1, buffer);
    }
    for (int i=0 ; i<5; ++i)
    {
    cout << array1 << endl;
    }
    return 0;
    }


    --
    Cheers,
    Robbie Hatley
    Tustin, CA, USA
    email: lonewolfintj at pacbell dot net
    web: home dot pacbell dot net slant earnur slant






    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
    Robbie Hatley, Jul 3, 2004
    #8
  9. Oskar

    JKop Guest

    Oskar posted:

    > Hi.
    > I`m new in cpp and i have a litlle problem.
    > i have a CString from Edit Box (eg."aaa bbb ccc 7327373 d feaf 323
    > dvjiv 234") and i want to put the data (space separated) into an
    > array.It shuld be somethign like this.
    >
    > array1[0]="aaa";
    > array1[1]="bbb";
    > ...
    > array1[43653]="234";
    >
    > The thing is i don`t have any clue how to do it. :|
    > Thx for help



    Why not just write a function?

    First of all, find out how many spaces there are.
    Then define an array via new. Then just take the addresses.


    Untest code but you'll get the jist:


    char* ReturnStringArray(const char* input_string)
    {
    size_t length = strlen(input_string);

    size_t amount_spaces = 0;

    for(size_t i = 0; i < length; ++i)
    {
    if (input_string == ' ') ++amount_spaces;
    }

    char* const p_stringarray = new char*[amount_spaces + 1];

    p_stringarray[0] = input_string;

    unsigned short index = 1;

    for(size_t i = 0; i < length; ++i)
    {
    if (input_string == ' ')
    {
    p_stringarray[index++] = input_string[i+1];
    }
    }

    p_stringarray[index] = 0;
    }


    int main()
    {
    char* p_blah = "aaaa bbbb cccc ffff kkkk";

    char* p_stringarray = ReturnStringArray(p_blah);

    //Work with it

    delete [] p_stringarray;
    }


    -JKop
    JKop, Jul 3, 2004
    #9
  10. Oskar

    Old Wolf Guest

    JKop <> wrote:
    > Oskar posted:
    >
    > > Hi.
    > > I`m new in cpp and i have a litlle problem.
    > > i have a CString from Edit Box (eg."aaa bbb ccc 7327373 d feaf 323
    > > dvjiv 234") and i want to put the data (space separated) into an
    > > array.It shuld be somethign like this.
    > >
    > > array1[0]="aaa";
    > > array1[1]="bbb";
    > > ...
    > > array1[43653]="234";
    > >
    > > The thing is i don`t have any clue how to do it. :|
    > > Thx for help

    >
    > Why not just write a function?
    >
    > First of all, find out how many spaces there are.
    > Then define an array via new. Then just take the addresses.


    What an awful idea. Why not make a vector, or some other container.

    [I have removed whitespace and unnecessary braces from the quoted text,
    so that the message is shorter]
    >
    > char* ReturnStringArray(const char* input_string)
    > {
    > size_t length = strlen(input_string);
    > size_t amount_spaces = 0;
    > for(size_t i = 0; i < length; ++i)
    > if (input_string == ' ') ++amount_spaces;
    >
    > char* const p_stringarray = new char*[amount_spaces + 1];


    "new char*" returns a char**

    > p_stringarray[0] = input_string;


    p_stringarray[0] is a char, input_string is a char const *

    > unsigned short index = 1;
    > for(size_t i = 0; i < length; ++i)
    > if (input_string == ' ')
    > p_stringarray[index++] = input_string[i+1];


    Will screw up when index overflows, use size_t for index

    > p_stringarray[index] = 0;


    Undefined behaviour - writes past the end of p_stringarray[]

    > }


    Undefined behaviour - no return value
    (it isn't clear what you intended to return, either. p_stringarray
    is the wrong type.)

    > int main()
    > {
    > char* p_blah = "aaaa bbbb cccc ffff kkkk";
    > char* p_stringarray = ReturnStringArray(p_blah);
    > //Work with it
    > delete [] p_stringarray;
    > }


    You've returned the first member of an array of pointers to the original
    string. In order to actually "work with it", you will have to parse
    what each pointer points to again, to find the space that ends the word.
    Also, this p_stringarray points to non-const char.

    All of these problems would never have arisen if you had advised
    the use of vector<string> instead.
    Old Wolf, Jul 4, 2004
    #10
  11. Oskar

    JKop Guest

    Good analysis. But I wrote that code in about 2 mins, paying minimal
    attention to detail. You may also note that I neglected to change the spaces
    to '\0'. But then I'd have to make the inputed non-const, or alternatively I
    could make a copy of it...



    -JKop
    JKop, Jul 4, 2004
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Smith

    Parsing char array to CString

    John Smith, Sep 6, 2003, in forum: C++
    Replies:
    4
    Views:
    7,218
    Pat McCormack
    Sep 9, 2003
  2. romi
    Replies:
    1
    Views:
    2,594
    Alf P. Steinbach
    Sep 2, 2004
  3. Tim  Wong
    Replies:
    5
    Views:
    21,944
    Tim Wong
    Jan 21, 2005
  4. romi
    Replies:
    2
    Views:
    625
    Joona I Palaste
    Sep 2, 2004
  5. Reversed
    Replies:
    0
    Views:
    323
    Reversed
    Jan 12, 2010
Loading...

Share This Page