473,508 Members | 2,400 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

CString and array.

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
Jul 22 '05 #1
8 2298
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
Jul 22 '05 #2

Użytkownik "JKop" <NU**@NULL.NULL> napisał w wiadomo¶ci
news:Hl*****************@news.indigo.ie...
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.
Jul 22 '05 #3
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

Jul 22 '05 #4

"Oskar" <os***@lanstorm.org> wrote in message
news:cc**********@atlantis.news.tpi.pl...
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


Jul 22 '05 #5
On Thu, 1 Jul 2004 15:33:20 +0200 in comp.lang.c++, "Oskar"
<os***@lanstorm.org> 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/gr*********....earthlink.net

See the welcome message posted twice per week in comp.lang.c++ or
available at http://www.slack.net/~shiva/welcome.txt

Jul 22 '05 #6
strtok function does what you want.
Jul 22 '05 #7

"Oskar" <os***@lanstorm.org> 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[i], buffer);
}
for (int i=0 ; i<5; ++i)
{
cout << array1[i] << 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 =---
Jul 22 '05 #8
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[i] == ' ') ++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[i] == ' ')
{
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
Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
649
by: romi | last post by:
Hi I have a problem, I want to convert program in Visual Basic 6 to VC++6. Program in VB works OK., but in VC++ I don't know how to convert 'VARIANT' to 'Cstring'. How to convert 'VARIANT ww' to...
5
14054
by: Tim Wong | last post by:
All: I am trying to convert a CString value to an unsigned char array. I found some code online that will allow me to compile, but when I try to print out...i get a whole mess. /*Begin Code*/...
3
9125
by: nsyforce | last post by:
What is the correct way to convert a const char* to a CString? I'm somewhat of a newbie and have tried several ways. While they all convert ok, I'm using a profiler that shows a memory leak for...
2
13159
by: jt | last post by:
Looking for an example how to convert and CString to an ASCII character string. Any examples on how to do this? Thank you, jt
7
8214
by: Klaus Bonadt | last post by:
I have an existing VC6 application using the MFC. I am able to pass CString and other parameters from such a VC6 dll to an unmanaged MFC dll (compiled in Visual Studio .NET). Now I want to use...
0
1078
by: Senapathy | last post by:
Environment : WinXP SP2, .NET 2003 Std edition I posted a query to the group some days back asking about how to identify stack corruption / array bounds or buffer overruns using .NET compiler...
4
11011
by: Cactus | last post by:
How to convert unsigned char* to CString: I wrote some function: u_char_ =55; u_char_ =66; u_char_ =77; .........=ii...... Convert_to_CS(u_char_);
1
42326
by: Tim Kelley | last post by:
I am new to Visual C++ and I am having difficulty parsing a string. We have a database that has a text field that contains multiple lines (seperated by a carriage return and new line characters). ...
2
2642
by: rag84dec | last post by:
Hi, i am using the CString array to hold some values... Is it right to do this.. CString t=new CString; But i am getting errors... Please help...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7114
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7377
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7034
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7488
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1544
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.