这些类型之间的相互转换,还有大家最好也来谈谈文件操作的问题!
比如:
AssignFile
Reset
ReWrite
Appden
CloseFile
//--------------------------------
FileOpen
FileCreate
FileClose
等这两类函数的用途和区别,最好有例子!
分不够再加,欢迎大家来讨论!

解决方案 »

  1.   

    Long string to PChar conversions are not automatic. Some of the differences between strings and PChars can make conversions problematic:
    Long strings are reference-counted, while PChars are not.
    Assigning to a string copies the data, while a PChar is a pointer to memory.
    Long strings are null-terminated and also contain the length of the string, while PChars are simply null-terminated.
      

  2.   

    我以前写过一篇文章是动态数组和字符串的
    http://www.csdn.net/develop/read_article.asp?id=14927
      

  3.   

    Use of the non-native Delphi language file handlers such as FileOpen is not encouraged. These routines map to system routines and return OS file handles, not normal Delphi file variables. These are low-level file access routines. For normal file operations use AssignFile, Rewrite, and Reset instead.
      

  4.   

    var
      p_char : pchar;
      chararray : array of char;
      bytearray : array of byte;
      str : string;
    begin
      p_char := AllocMem(512);
      ZeroMemory(p_char,512);
      str := '1234567890';
      setlength(chararray,length(str));
      setlength(bytearray,length(str));
      copymemory(chararray,pchar(str),length(str));
      copymemory(p_char,pchar(str),length(str));
      copymemory(bytearray,pchar(str),length(str));
    或:
      for i:= 0 to length(str)-1 do
      begin
        chararray[i] := str[i+1];
        p_char^ := str[i+1];
        Inc(p_char);
        bytearray[i] := byte(str[i+1]);
      end;
      
    end;
      

  5.   

    procedure TForm1.Button1Click(Sender: TObject);
    test:array[0..1] of string = ('小浩子','試試');
    begin
     edit1.Text:=test[0]+test[1];
    end;
      

  6.   

    F1AssignFile, OpenDialog, Readln, CloseFile Examplevar 
      F: TextFile;
      S: string;
    begin
      if OpenDialog1.Execute then            { Display Open dialog box }
      begin
        AssignFile(F, OpenDialog1.FileName); { File selected in dialog }
        Reset(F);
        Readln(F, S);                        { Read first line of file }
        Edit1.Text := S;                     { Put string in a TEdit control }
        CloseFile(F);
      end;
    end;
    Rewrite Examplevar
      F: TextFile;
    begin
      AssignFile(F, 'NEWFILE.$$$');
      Rewrite(F);
      Writeln(F, 'Just created file with this text in it...');
      CloseFile(F);
    end;
    Append, Flush Examplevar
      f: TextFile;
    begin
      if OpenDialog1.Execute then
      begin                    { open a text file }
        AssignFile(f, OpenDialog1.FileName);
        Append(f);
        Writeln(f, 'I am appending some stuff to the end of the file.'); 
        { insert code here that would require a Flush before closing the file }
        Flush(f);  { ensures that the text was actually written to file }
        CloseFile(f);
      end;
    end;
    FileOpen Exampleprocedure OpenForShare(const FileName: String);var
      FileHandle : Integer;
    begin
      FileHandle := FileOpen(FileName, fmOpenWrite or fmShareDenyNone);
      if FileHandle > 0 then
        {valid file handle}
      else
        {Open error: FileHandle = negative DOS error code}
    end;
    FileExists, RenameFile, FileCreate, FileWrite, FileClose, ExtractFileName Exampleprocedure TForm1.Button1Click(Sender: TObject);
    var
      BackupName: string;
      FileHandle: Integer;
      StringLen: Integer;
      X: Integer;
      Y: Integer;
    begin
      if SaveDialog1.Execute then
      begin
        if FileExists(SaveDialog1.FileName) then
        begin
          BackupName := ExtractFileName(SaveDialog1.FileName);
          BackupName := ChangeFileExt(BackupName, '.BAK');
          if not RenameFile(SaveDialog1.FileName, BackupName) then
            raise Exception.Create('Unable to create backup file.');
        end;
        FileHandle := FileCreate(SaveDialog1.FileName);
        { Write out the number of rows and columns in the grid. }
        FileWrite(FileHandle, 
          StringGrid1.ColCount, SizeOf(StringGrid1.ColCount));
        FileWrite(FileHandle, 
          StringGrid1.RowCount, SizeOf(StringGrid1.RowCount));
        for X := 0 to StringGrid1.ColCount ?1 do
        begin
          for Y := 0 to StringGrid1.RowCount ?1 do
          begin
            { Write out the length of each string, followed by the string itself. }
            StringLen := Length(StringGrid1.Cells[X,Y]);
            FileWrite(FileHandle, StringLen, SizeOf(StringLen));
            FileWrite(FileHandle,
              StringGrid1.Cells[X,Y], Length(StringGrid1.Cells[X,Y]);
          end;
        end;
        FileClose(FileHandle);
      end;
    end;
      

  7.   

    var
      p_char : pchar;
      chararray : array of char;
      bytearray : array of byte;
      str : string
    begin
      p_char := AllocMem(512);
      zeromemory(p_char,512);
      str := '1234567890';
      setlength(chararray,length(str));
      setlength(bytearray,length(str));
      copymemory(chararray,pchar(str),length(str));
      copymemory(bytearray,pchar(str),length(str));
      copymemory(p_char,pchar(str),length(str));
    或:
      for i:= 0 to length(str)-1 do
      begin
        chararray[i] := str[i+1];
        bytearray[i] := byte(str[i+1]);
        p_char^ := str[i+1];
        Inc(p_char);
      end;
      FreeMem(p_char);
      

  8.   

    F: TextFile;
    begin
      try
        AssignFile(F, '.\ErrorLog.txt');
        if fileexists('.\ErrorLog.txt') then
           Append(F)
        else
           ReWrite(F);
        Writeln(F, errmsg);
        Flush(F);
      finally
        CloseFile(F);
      end;
    end;
      

  9.   

    呵呵,assign那些是对文件进行操作的,带file的那些可以对文件进行操作。我总感觉file的那些好像是类似于dos下命令行。assign那些负责对文件进行读写
    此外,类型转换的问题 PChar,Array of Char,Array of Byte,String之间的相互转换问题
    pchar array of char string 只见可以很容易的转换
    pchar(str:string)  string(p:pchar)  pchar和array of char可以直接赋值给string
    ......
      

  10.   

    F: File of char;
    a: char;
    begin
      try
        AssignFile(F, '.\ErrorLog.txt');
        reset(F);
        read(F,a);  //每次读一个字节
      finally
        CloseFile(F);
      end;
    end;
      

  11.   

    procedure TForm1.Button1Click(Sender: TObject);var
      iFileHandle: Integer;
      iFileLength: Integer;
      iBytesRead: Integer;
      Buffer: PChar;
      i: Integer;
    begin
      if OpenDialog1.Execute then
      begin
        try
          iFileHandle := FileOpen(OpenDialog1.FileName, fmOpenRead);
          iFileLength := FileSeek(iFileHandle,0,2);
          FileSeek(iFileHandle,0,0);
          Buffer := PChar(AllocMem(iFileLength + 1));
          iBytesRead := FileRead(iFileHandle, Buffer, iFileLength);
          FileClose(iFileHandle);
          ShowMessage(Buffer[2]);
        finally
          FreeMem(Buffer);
        end;
      end;
    end;兄弟们,帮我看看这个程序错在那里!
    谢谢了。
      

  12.   

    procedure TForm1.Button2Click(Sender: TObject);
    var
      nFile:Integer;
      nFileSize:Cardinal;
      nReadSize:Cardinal;
      InBuffer:PChar;
      OutBuffer:PChar;
    begin
      if (OpenDialog1.Execute) then
      begin
        nFile:=FileOpen(OpenDialog1.FileName,fmOpenReadWrite);
        if (nFile>0) then
        begin
          nFileSize:=FileSeek(nFile,0,2);
          FileSeek(nFile,0,0);
          GetMem(PChar(InBuffer),nFileSize);
          GetMem(PChar(OutBuffer),nFileSize);
          ZeroMemory(InBuffer,nFileSize);
          ZeroMemory(OutBuffer,nFileSize);
          nReadSize:=FileRead(nFile,InBuffer^,nFileSize);
          lbDES1.EncryptBuffer(InBuffer^,nFileSize,OutBuffer^);
          FileSeek(nFile,0,0);
          FileWrite(nFile,OutBuffer^,nReadSize);
          FileClose(nFile);
          FreeMem(InBuffer);
          FreeMem(OutBuffer);
          ShowMessage('加密成功!');
        end;
      end;
    end;procedure TForm1.Button3Click(Sender: TObject);
    var
      nFile:Integer;
      nFileSize:Cardinal;
      nReadSize:Cardinal;
      InBuffer:PChar;
      OutBuffer:PChar;
    begin
      if (OpenDialog1.Execute) then
      begin
        nFile:=FileOpen(OpenDialog1.FileName,fmOpenReadWrite);
        if (nFile>0) then
        begin
          nFileSize:=FileSeek(nFile,0,2);
          FileSeek(nFile,0,0);
          GetMem(PChar(InBuffer),nFileSize);
          GetMem(PChar(OutBuffer),nFileSize);
          ZeroMemory(InBuffer,nFileSize);
          ZeroMemory(OutBuffer,nFileSize);
          nReadSize:=FileRead(nFile,InBuffer^,nFileSize);
          lbDES1.DecryptBuffer(InBuffer^,nFileSize,OutBuffer^);
          FileSeek(nFile,0,0);
          FileWrite(nFile,OutBuffer^,nReadSize);
          FileClose(nFile);
          FreeMem(InBuffer);
          FreeMem(OutBuffer);
          ShowMessage('解密成功!');
        end;
      end;
    end;这两个函数有一点问题,请各位帮帮忙!
      

  13.   

    To qinmaofan(采菊南山下):
    我这只是主要做文件操作,真正的加密还不是这些!