Hello,
I must convert AnsiString in char * I have done:
AnsiString mesliasses=AnsiString("MesLiasses");
source1=new
char[sizeof(strcat(ExtractFilePath(Application->ExeName).c_str(),mesliasses.c_str()))+1];
target1=new char[sizeof(strcat(ChVista.c_str(),mesliasses.c_str()))+1];
SourceDirectory=new
char[sizeof(strcat(ExtractFilePath(Application->ExeName).c_str()
,mesliasses.c_str()))+1];
TargetDirectory=new
char[sizeof(strcat(ChVista.c_str(),mesliasses.c_str()))+1];
source1=strcat(ExtractFilePath(Application->ExeName).c_str()
,mesliasses.c_str());
target1=strcat(ChVista.c_str(),mesliasses.c_str());
strcpy(source1,SourceDirectory);
strcpy(target1,TargetDirectory);
CopyTree(SourceDirectory,TargetDirectory);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Compilation good, build goor, running I obtain an acces violation message.
I have tried other but always errors
If you ha ve an idea or know what I'm doing wrong Thank you very much
Patrick
Thank you at all,
Thank you Denis to have seen!!!
Thank you at all again
Patrick
"patrickartaud" a écrit dans le message de
news:47850d32@newsgroups.borland.com...
> Hello,
> I must convert AnsiString in char * I have done:
> AnsiString mesliasses=AnsiString("MesLiasses");
> source1=new
> char[sizeof(strcat(ExtractFilePath(Application->ExeName).c_str(),mesliasses.c_str()))+1];
> target1=new char[sizeof(strcat(ChVista.c_str(),mesliasses.c_str()))+1];
> SourceDirectory=new
> char[sizeof(strcat(ExtractFilePath(Application->ExeName).c_str()
> ,mesliasses.c_str()))+1];
> TargetDirectory=new
> char[sizeof(strcat(ChVista.c_str(),mesliasses.c_str()))+1];
> source1=strcat(ExtractFilePath(Application->ExeName).c_str()
> ,mesliasses.c_str());
> target1=strcat(ChVista.c_str(),mesliasses.c_str());
> strcpy(source1,SourceDirectory);
> strcpy(target1,TargetDirectory);
> CopyTree(SourceDirectory,TargetDirectory);
> //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
> Compilation good, build goor, running I obtain an acces violation message.
> I have tried other but always errors
> If you ha ve an idea or know what I'm doing wrong Thank you very much
> Patrick
Hi Chris
Chris Uzdavinis (TeamB) says:
> Asger Joergensen writes:
>
> > Hi patrickartaud
> >
> > Whooow, some difficult code...;-)
> >
> > try somethng like this, I havent done it all, but I'm
> > sure that You get the idea.
> >
> > String mesliasses = String("MesLiasses");
> > String Tmp = ExtractFilePath(Application->ExeName) + mesliasses;
> > char* source1 = new char[Tmp.Length()+1];
> > strcpy(source1, Tmp.c_str());
>
> Much better indeed, but I still quesion the idea of using any char*
> variables at all.
In this case I would not use char* my self, but I don't know
the guy, he might have his reasons, so I just showd one way
to do what he was doing.
I leave the long explanations to the clever guys...
p.s. The first line should have been:
String mesliasses = "MesLiasses";
Kind regards
Asger
Asger Joergensen writes:
> Hi patrickartaud
>
> Whooow, some difficult code...;-)
>
> try somethng like this, I havent done it all, but I'm
> sure that You get the idea.
>
> String mesliasses = String("MesLiasses");
> String Tmp = ExtractFilePath(Application->ExeName) + mesliasses;
> char* source1 = new char[Tmp.Length()+1];
> strcpy(source1, Tmp.c_str());
Much better indeed, but I still quesion the idea of using any char*
variables at all.
--
Chris (TeamB);
"patrickartaud" writes:
> Hello,
> I must convert AnsiString in char * I have done:
> AnsiString mesliasses=AnsiString("MesLiasses");
Ok, but you could just write this and avoid as much typing, and avoid
constructing a temporary object:
AnsiString mesliasses("MesLiasses");
> source1=new
> char[sizeof(strcat(ExtractFilePath(Application->ExeName).c_str(),mesliasses.c_str()))+1];
Now this is a problem. For one, the sizeof operator doesn't tell you
length of runtime values, it tells you the number of bytes that a
static type requires. strcat returns a pointer, and pointers (with
this compiler) are 4 bytes. Always. You are wanting to know how long
the string is that starts at the address that the pointer holds.
But first, the most serious logic bug you have: strcat appends bytes
to an existing array of characters. You must ensure that 1) you are
allowed to write into the memory you pass to it, and 2) there is
enough extra bytes at the end to hold whatever value you are trying to
append.
Unfortunately, neither of the above requirements are met in your call
to strcat. Since ExtractFilePath returns an AnsiString, and c_str()
accesses internal memory of that class, you do not own the memory and
have no business writing into it. Further, you do not and cannot know
how much memory is available for writing after the null in that
string.
The only saving grace here is that expressions in a sizeof operator
are not actually evaluated, since only the resulting type is necessary
to calculate the size, and that value must be available at
compile-time. Thus the above code is equivalent to:
source1 = new char[4].
But surely that wasn't what you were trying to do. Instead:
AnsiString exename(ExtractFilePath(Application->ExeName) + mesliasses;
source1 = new char[exename.Length()];
But now I wonder, why are you wanting a dynamically allocated
character array for working with "strings" when you could be using
true string objects to do all the memory dirty-work for you?
> target1=new char[sizeof(strcat(ChVista.c_str(),mesliasses.c_str()))+1];
Same problem here. Except this is worse, because if this new
expression throws, then you have leaked the memory pointed to by
source1.
Using true string classes would remove this exception-unsafe code
problem as well.
> SourceDirectory=new
> char[sizeof(strcat(ExtractFilePath(Application->ExeName).c_str()
,mesliasses.c_str()))+1];
Same problem here, except if THIS expression throws, you leak source1
and target1.
> TargetDirectory=new
> char[sizeof(strcat(ChVista.c_str(),mesliasses.c_str()))+1];
> source1=strcat(ExtractFilePath(Application->ExeName).c_str()
> ,mesliasses.c_str());
Same bug here, except if *this* expression throws, you leak source1,
target1, and SourceDirectory.
> target1=strcat(ChVista.c_str(),mesliasses.c_str());
You cannot do this, since you don't know how the memory is which you
will be appending, and target1 is probably way too small anyway.
Again, a string object would help here.
> strcpy(source1,SourceDirectory);
Another bug.
> strcpy(target1,TargetDirectory);
Another Bug.
> CopyTree(SourceDirectory,TargetDirectory);
In short, please work with AnsiStrings and no char arrays or pointers
in this situation.
--
Chris (TeamB);
"Remy Lebeau (TeamB)" wrote in message
news:47852090$1@newsgroups.borland.com...
>
> If you change CopyTree() to accept AnsiString parameters instead of char*
> parameters, then you can get rid of the temp variables altogether:
>
> AnsiString mesliasses = "MesLiasses";
> CopyTree(ExtractFilePath(Application->ExeName) + mesliasses, ChVista +
> mesliasses);
Patrick,
If you are using the CopyTree() function I sent you, then like Remy said,
you can use AnsiString parameters instead by just adding a wrapper function:
bool CopyTree( const AnsiString &SourceDirectory, const AnsiString
&TargetDirectory )
{
// now invoke the original CopyTree
CopyTree( SourceDirectory.c_str(), TargetDirectory.c_str() );
}
- Dennis
"patrickartaud" wrote in message
news:47850d32@newsgroups.borland.com...
> I must convert AnsiString in char * I have done:
That code is completely wrong, and quite dangerous, for what you are
attempting. Not only can strcat() NOT modify an AnsiString properly, but
you are using sizeof() when you should be using strlen() instead, and your
parameters to strcpy() are backwards.
Try this alternative safer, cleaner, code instead:
AnsiString mesliasses = "MesLiasses";
AnsiString SourceDirectory = ExtractFilePath(Application->ExeName) +
mesliasses;
AnsiString TargetDirectory = ChVista + mesliasses;
CopyTree(SourceDirectory.c_str(), TargetDirectory.c_str());
If you change CopyTree() to accept AnsiString parameters instead of char*
parameters, then you can get rid of the temp variables altogether:
AnsiString mesliasses = "MesLiasses";
CopyTree(ExtractFilePath(Application->ExeName) + mesliasses, ChVista +
mesliasses);
Gambit
"patrickartaud" wrote in message
news:47850d32@newsgroups.borland.com...
> Hello,
> I must convert AnsiString in char * I have done:
> AnsiString mesliasses=AnsiString("MesLiasses");
> source1=new
> char[sizeof(strcat(ExtractFilePath(Application->ExeName).c_str(),mesliasses.c_str()))+1];
> target1=new char[sizeof(strcat(ChVista.c_str(),mesliasses.c_str()))+1];
> SourceDirectory=new
> char[sizeof(strcat(ExtractFilePath(Application->ExeName).c_str()
> ,mesliasses.c_str()))+1];
> TargetDirectory=new
> char[sizeof(strcat(ChVista.c_str(),mesliasses.c_str()))+1];
> source1=strcat(ExtractFilePath(Application->ExeName).c_str()
> ,mesliasses.c_str());
> target1=strcat(ChVista.c_str(),mesliasses.c_str());
> strcpy(source1,SourceDirectory);
> strcpy(target1,TargetDirectory);
> CopyTree(SourceDirectory,TargetDirectory);
> //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
> Compilation good, build goor, running I obtain an acces violation message.
> I have tried other but always errors
> If you ha ve an idea or know what I'm doing wrong Thank you very much
> Patrick
You can't use the c-string routines like strcat() to modify an AnsiString.
--
Bruce
Hi patrickartaud
Whooow, some difficult code...;-)
try somethng like this, I havent done it all, but I'm
sure that You get the idea.
String mesliasses = String("MesLiasses");
String Tmp = ExtractFilePath(Application->ExeName) + mesliasses;
char* source1 = new char[Tmp.Length()+1];
strcpy(source1, Tmp.c_str());
Kind regards
Asger