Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert AnsiString to Const char

Status
Not open for further replies.

davidmelvin

Technical User
Nov 3, 2000
13
US
Hello,

I'm trying to use CopyFile to copy some foles from one location on a drive to another location on another drive. The CopyTo folder is defined by information the user places in a ComboBox. When I try to use ComboBox1->Text for the lpszNewFile, I get an error "can't convert AnsiString to const char".

How can I use the information in the ComboBox Text property in the CopyFile function?
 
do you get
can't convert AnsiString to const char
or
can't convert AnsiString to const char* ?

Ion Filipski
1c.bmp
 
Try "ComboBox1->Text for the lpszNewFile.c_str()" or "(const char*)ComboBox1->Text for the lpszNewFile.c_str()"

Totte
Keep making it perfect and it will end up broken.
 
I've tried several configurations and get about the same error. The code looks like this.

CopyFile("C:\\temp\\AcPro8.exe", ComboBox1->Text, false);

And I get the following errors.
[C++ Error] AcP8SPU1.cpp(170): E2034 Cannot convert 'AnsiString' to 'const char *'

[C++ Error] AcP8SPU1.cpp(170): E2342 Type mismatch in parameter 'lpNewFileName' (wanted 'const char *', got 'AnsiString')

I've also tried

CopyFile("C:\\temp\\AcPro8.exe", (const char *)ComboBox1->Text, false);

I get the following error with this code.
[C++ Error] AcP8SPU1.cpp(170): E2031 Cannot cast from 'AnsiString' to 'const char *'
 
Dear david...
So, just go a little step back and make a .c_str() (which is a const char*, as far as I know) from the text , how totte suggested.
either You have a string of char which is as long as Your
comboboxtext:

char* mycopystring = new char[ComboBox->Text.Length()+1];
and then
strcpy(mycopystring, ComboBox->Text.c_str() );
and then
CopyFile("C:\\temp\\AcPro8.exe", mycopystring, false);
and don't forget
delete mycopystring;

but I think this is what You meant, Totte?
or You try to place the combobox.c_str() right into copyfile:

CopyFile("C:\\temp\\AcPro8.exe", ComboBox->Text.c_str(), false);

Best regards,

Michael
 
Thank you Michael. Problem solved.

I'd also like to thank IonFilipski and Tottle. Thanks for the quick response.

Regards,
David
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top