Board index » delphi » Incompatible types 'string' and 'pchar'

Incompatible types 'string' and 'pchar'

Help!

The following code worked well under Delphi 1.0 but doesn't compile under 2.0

   Procedure CreateBackup(Const Filename : String);
   Var
      Filename, BackupFilename, BackupExt: String;}
   Begin
     BackupExt := '.Bak';
     Backupfilename := ChangeFileExt(filename, BackupExt);
     DeleteFile(BackupFilename);
     RenameFile(Filename, Backupfilename);
   end;  {of CreateBackup}

It generates compiler error 208 = Incompatible types 'string' and 'Pchar'
on the DeleteFile line.

The filename variable which is passed to DeleteFile() is supposed to be of
type 'string' and I don't see where the 'Pchar' type comes into it at all??

The old file will not be renamed to the new extension unless the previous
backup has been deleted (it won't overwrite the old file).

Does anyone have a way of backing up a file which works??

--
Email:  bre...@doolittle.vetsci.su.oz.au (Brett Jones)
M.C. Franklin Laboratory, Department of Animal Science
University of Sydney, Camden, N.S.W. 2570, Australia
Ph: +61 46 550257   Fax: +61 46 556657   Mob: +61 18 119968

 

Re:Incompatible types 'string' and 'pchar'


Quote
Brett Jones wrote:

> Help!

> The following code worked well under Delphi 1.0 but doesn't compile under 2.0

>    Procedure CreateBackup(Const Filename : String);
>    Var
>       Filename, BackupFilename, BackupExt: String;}
>    Begin
>      BackupExt := '.Bak';
>      Backupfilename := ChangeFileExt(filename, BackupExt);
>      DeleteFile(BackupFilename);
>      RenameFile(Filename, Backupfilename);
>    end;  {of CreateBackup}

> It generates compiler error 208 = Incompatible types 'string' and 'Pchar'
> on the DeleteFile line.

> The filename variable which is passed to DeleteFile() is supposed to be of
> type 'string' and I don't see where the 'Pchar' type comes into it at all??

> The old file will not be renamed to the new extension unless the previous
> backup has been deleted (it won't overwrite the old file).

> Does anyone have a way of backing up a file which works??

> --
> Email:  bre...@doolittle.vetsci.su.oz.au (Brett Jones)
> M.C. Franklin Laboratory, Department of Animal Science
> University of Sydney, Camden, N.S.W. 2570, Australia
> Ph: +61 46 550257   Fax: +61 46 556657   Mob: +61 18 119968

Hello!
If your string variables are D2's dynamically allocated String type,
then you can just typecast like: PChar(BackupFilename)

Atle Markeng

Re:Incompatible types 'string' and 'pchar'


Quote
> Brett Jones <bre...@rancho.camden.usyd.edu.au> wrote in article

<31D0B949.1...@rancho.camden.usyd.edu.au>...

Quote

> The following code worked well under Delphi 1.0 but doesn't compile
under 2.0

>    Procedure CreateBackup(Const Filename : String);
>    Var
>       Filename, BackupFilename, BackupExt: String;}
>    Begin
>      BackupExt := '.Bak';
>      Backupfilename := ChangeFileExt(filename, BackupExt);
>      DeleteFile(BackupFilename);
>      RenameFile(Filename, Backupfilename);
>    end;  {of CreateBackup}

> It generates compiler error 208 = Incompatible types 'string' and
'Pchar'
> on the DeleteFile line.

I had this problem a while back, also.  The DeleteFile() function has
changed
between the D1 and D2.  It now uses type PChar, instead of string for its
parameter.  You will need to convert BackupFilename to a PChar first,
before
you make the call to DeleteFile.  Search the help files for StrPCopy.

Re:Incompatible types 'string' and 'pchar'


Quote
David S. Becker wrote:

> > datamar <data...@login.eunet.no> wrote in article
> <31D0F3B9.3...@login.eunet.no>...
> > Brett Jones wrote:
> > > The following code worked well under Delphi 1.0 but doesn't compile
> under 2.0
> [snip]
> > > It generates compiler error 208 = Incompatible types 'string' and
> 'Pchar'
> > > on the DeleteFile line.
> [snip]
> > If your string variables are D2's dynamically allocated String type,
> > then you can just typecast like: PChar(BackupFilename)

> Bzzt.  This problem is somewhat of an FAQ (heck, even *I* know the answer,
> and I don't develop in D2 yet)...  There is a Win32 API call named
> DeleteFile() and you are getting a name clash between the API and the
> SysUtils unit.  Simply scope your call to DeleteFile() like so:
> SysUtils.DeleteFile() and the problem should go away.

        I didn't know that, although it was my first guess. I noticed a little
while ago that there's a Win32 CopyFile function, which may change people's
answers to "how do I copy a file in Delphi?" or maybe not.
        Anybody have any idea why these things are suddenly there in Win32
(or why they weren't there in win 3.1?)

--
David Ullrich
Sig file accidentally deleted - sorry.

Re:Incompatible types 'string' and 'pchar'


Quote
Brett Jones <bre...@rancho.camden.usyd.edu.au> wrote:
>The filename variable which is passed to DeleteFile() is supposed to be of
>type 'string' and I don't see where the 'Pchar' type comes into it at all??
>The old file will not be renamed to the new extension unless the previous
>backup has been deleted (it won't overwrite the old file).
>Does anyone have a way of backing up a file which works??

As I remember this is very very easy to solve.
There are to "DeleteFile" functions. One in SysUtils and one in
Windows (I believe). Switch the order of these units  in your uses
clause and it should work. Alternatively you could refer to
SysUtils.DelteFile

(Or was it System and WinProcs, or was it.......)

Hope this helps
Meik Weber
m.we...@public.ndh.com or
100744.3...@compuserve.com

Re:Incompatible types 'string' and 'pchar'


Quote
>>      DeleteFile(BackupFilename);
>I had this problem a while back, also.  The DeleteFile() function has
>changed
>between the D1 and D2.  It now uses type PChar, instead of string for its
>parameter.  You will need to convert BackupFilename to a PChar first,
>before
>you make the call to DeleteFile.  Search the help files for StrPCopy.

It is easier to typecast:
    DeleteFile(PChar(BackupFilename));
Luis

Other Threads