Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

String of data into an array

20 views
Skip to first unread message

Sudzzz

unread,
Jun 25, 2007, 3:15:00 PM6/25/07
to
Hi,
I'm relatively new to programming and would like to convert a string
of data that is extracted from a program into an array.
My string of data looks like this.

{105,0,196,3,54,154,12,53,125,46}

I would like to convert this char string into an array. Please help me
out.

Thanks,
Sudeep
http://sudeeppillai.blogspot.com

BobR

unread,
Jun 25, 2007, 5:46:17 PM6/25/07
to

Sudzzz wrote in message...

> Hi,
> I'm relatively new to programming and would like to convert a string
> of data that is extracted from a program into an array.
> My string of data looks like this.
>
> {105,0,196,3,54,154,12,53,125,46}
>
> I would like to convert this char string into an array. Please help me
> out.

int array[] = {105,0,196,3,54,154,12,53,125,46}; // <G>

Seriously, look into std::stringstream.
A rough example:

#include <iostream>
#include <string>
#include <sstream>
#include <vector>

int main(){
// std::istringstream in( "105,0,196,3,54,154,12,53,125,46" );
std::string stin( "105,0,196,3,54,154,12,53,125,46" );
std::istringstream in( stin );
std::vector<int> array;

for( std::string line; std::getline( in, line, ',' ); ){
std::stringstream conv( line );
int num(0);
conv >> num;
array.push_back( num );
conv.clear(); conv.str("");
} // for(getline)

for( std::size_t i(0); i < array.size(); ++i ){
std::cout<< array.at( i ) <<std::endl;
} // for(i)
} // main()
/* - output -
105
0
196
3
54
154
12
53
125
46
*/

--
Bob R
POVrookie


0 new messages