How to convert String to AnsiString
Dear all
I got one problem with builder c++ AnsiString to string.
when i use ifstream this arg to read file from local system
and then after in i want to show message in my AP which i can called
ShowMessage this method and then in this method only support
AnsiString so on .
i need to convert string to AnsiString in showmessage. i hope someone can give one right direction to fix this problem
thank's
Code:
void __fastcall TForm1::Button1Click(TObject *Sender)
{ string str;
AnsiString s;
ifstream fin;
fin.open("config.ini");
if(fin.is_open()){
ShowMessage("a");
while(getline(fin,str)){
s = AnsiString(); // convert string to AnsiString
//ShowMessage("a" + s);
}
}
}
Re: How to convert String to AnsiString
Quote:
Originally Posted by roger5089
i need to convert string to AnsiString in showmessage.
Convert a std::string to a LPCSTR (const char *) using std::string::c_str () method
Like this -
Code:
std::string strTest ("This is a test string");
const char* pszConverted = strTest.c_str ();
Re: How to convert String to AnsiString
Code:
string s("gfd");
AnsiString s = AnsiString (s.c_str());
Re: How to convert String to AnsiString
Quote:
Originally Posted by Odiee
Code:
string s("gfd");
AnsiString s = AnsiString (s.c_str());
Sorry for a "silly" question... What is the AnsiString ? I haven't found it in MSDN... :confused:
______________
Ahhh! Never mind!
It is C++ Builder, not a MS! :D
Re: How to convert String to AnsiString
Thank's everybody help
yes that's c++ builder method called AnsiString , but that problem is
in c++ builder any compoment support AnsiString not for string , so i need to find out any convert i can convert string to AnsiString
something like it
Code:
string str1 = "1.roger 2.chunag 3.abc 4.ccc";
replace(str1.begin(), str1.end(), ' ', '\n');
Memo1->Text = str1 ; // it wasnot work becuase cannot convert string to ansistring
the output of Memo1 i want to be like this one
1.roger
2.chuang
3.abc
4.ccc
Re: How to convert String to AnsiString
[QUOTE=roger5089]Thank's everybody help
yes that's c++ builder method called AnsiString , but that problem is
in c++ builder any compoment support AnsiString not for string , so i need to find out any convert i can convert string to AnsiString
something like it
Code:
string str1 = "1.roger 2.chunag 3.abc 4.ccc";
replace(str1.begin(), str1.end(), ' ', '\n');
Memo1->Text = str1 ; // it wasnot work becuase cannot convert string to ansistring
the output of Memo1 i want to be like this one
1.roger
2.chuang
3.abc
4.ccc
Re: How to convert String to AnsiString
The way you are using the replace method for your string variable will not work correctly. It's better to iterate over your string, and using Builder's TMemo->Lines->Append function breaking the String up something like this works...
string str1 = "1.roger 2.chunag 3.abc 4.ccc";
AnsiString newline;
for(int i=0; i<str1.length(); i++)
{
if(str1[i]!=' ')
newline=newline+str1[i];
else
{
Memo1->Lines->Append(newline);
newline="";
}
}
Memo1->Lines->Append(newline);
Re: How to convert String to AnsiString
Quote:
Originally Posted by roger5089
Thank's everybody help
yes that's c++ builder method called AnsiString ,
So why did you post in the Visual C++ Programming forum?
You should have posted to the Non-Visual C++ forum. Questions concerning Borland C++ builder's library are off-topic here.
Regards,
Paul McKenzie
Re: How to convert String to AnsiString
[ Moved Thread ]
@ WinstonFahrenheit: In case you haven't noticed, this is 2+ years old thread.