954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

String to array of char

Hello,, I'v had some problem,,, when i read a file,using visual C++ 2005.

example:
a.txt
111,222,333,
444,555,666

with this code :
StreamReader^ din = File::OpenText(fileName);
String^ str;
int count = 0;
while ((str = din->ReadLine()) != nullptr)
{
count++;
Console::WriteLine("line {0}: {1}", count, str );
}

How can I, convert str, from string to char(array of char).
cause, if in array, I can easy split the content of that file.


thanks..

VIkhers
Newbie Poster
8 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

I think what you're looking for is:

std::string Test = "hello world";
char* NewVar = Test.c_str();


Although I think you'd be better off using an std::stringstream.

daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204
 

>>How can I, convert str, from string to char(array of char).
cause, if in array, I can easy split the content of that file.

What is it that you want to do with that String? Have you looked at the methods it provides to see if it already does what you want to do?

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

dear, Mr. David...
I'v tried but it's still not work.
there's an error :: error C2440:'initilizing' : cannot convert from'const char *' to 'System::Byte^'

I want to split the string per line sir: example:
str = 111,222,333
i want it be(after I remove the delimiter ','):
111
222
333
so, it easy if the str, i convert to an array of char.
i'v tried this:
marshal_context ^ context = gcnew marshal_context();
const char* str4 = context->marshal_as(str);
puts(str4);

but it's still not work.
thanks.

VIkhers
Newbie Poster
8 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

dear, Mr. David... I'v tried but it's still not work. there's an error :: error C2440:'initilizing' : cannot convert from'const char *' to 'System::Byte^'

I want to split the string per line sir: example: str = 111,222,333 i want it be(after I remove the delimiter ','): 111 222 333 so, it easy if the str, i convert to an array of char. i'v tried this: marshal_context ^ context = gcnew marshal_context(); const char* str4 = context->marshal_as(str); puts(str4);

but it's still not work. thanks.

Firstly why do you want to read the complete line and then split it with delimiter as "," ...???

Why not directly use

istream::getline(char* s,streamsize n,',');


with this you can directly read only upto the "," and hence get it in the format you actually want rather than reading the whole line first and then splitting it.

csurfer
Posting Pro
568 posts since Jan 2009
Reputation Points: 485
Solved Threads: 88
 

Example: Found most of this here

using namespace System;
using namespace System::Collections;

int main()
{
   String^ words = "111,222,333";
   Char chars[] = {' ', ', ', '->', ':'};
   array<String^>^ split = words->Split(',');
   for(int i = 0; i < split->Length; i++)
       Console::Write(  "\t{0}\n", split[ i ] );
}
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

ok sir,, that's all I want need.
and then I want to save the output to database.
n It's works....

thanks a lot for all.....
(thks moderator)

VIkhers
Newbie Poster
8 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You