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..

Recommended Answers

All 6 Replies

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.

>>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?

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<const char*>(str);
puts(str4);

but it's still not work.
thanks.

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<const char*>(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.

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 ] );
}

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)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.