我发现在写文件的时候,实际写的内容跟我想写的内容怎么不一样??
我简化了我的程序,原理就是取得写的内容到数组中,然后写入到文件。
下面是简化的程序,应该是写一个";本"的内容,可是结果却不是,怎么回事??
;的编码是59,本的编码177 190
请问我的实现方法有问题吗??
var
    Handle:Integer;
    buf:Array of Byte;
begin
    setLength(buf,3);
    buf[0]:=59;
    buf[1]:=177;
    buf[2]:=190;
    Handle:=FileCreate('c:\aa.ini');
    FileWrite(Handle,buf,3);
    FileClose(Handle);
end;

解决方案 »

  1.   

    发现问题了,写文件的时候,必须一个字节一个字节的写?
    下面这个代码可以实现,原来问题是处在FileWrite函数的使用上。
    var
        Handle:Integer;
        buf:Array of Byte;
        index:Integer;
    begin
        setLength(buf,1);
        buf[0]:=59;
        buf[1]:=177;
        buf[2]:=190;
        if FileExists('c:\aa.ini') then DeleteFile('c:\aa.ini');
        Handle:=FileCreate('c:\aa.ini');
        for index:=0 to 2 do
        begin
          FileSeek(Handle,0,2);
          FileWrite(Handle,buf[index],1);
        end;
        FileClose(Handle);
    end;请问能执行一次FileWrite就把数组中的内容全部写进去吗??或者有什么替代的方法把数组转换成缓冲区,然后一次写入?? 不知道我说得有人能理解不??
      

  2.   

    指针传错了
    应该是:
     FileWrite(Handle,buf[0],3);
      

  3.   

    高手能不能解释一下为什么
    FileWrite(Handle,buf,3);
    不行,而
        for index:=0 to 2 do
        begin
          FileSeek(Handle,0,2);
          FileWrite(Handle,buf[index],1);
        end;
    可以呢??
      

  4.   

    delphi的FileWrite第三个参数为写入缓冲的首址,如果你写
    FileWrite(Handle,buf,3); 
    那么传入的是buf本身的地址,而不是指向缓冲的首址,用buf[0]就可以了
      

  5.   

    打错了,应该是第二个参数,你的程序这样没问题:
    var
        Handle:Integer;
        buf:Array of Byte;
    begin
        setLength(buf,3);
        buf[0]:=59;
        buf[1]:=177;
        buf[2]:=190;
        Handle:=FileCreate('c:\aa.ini');
        FileWrite(Handle,buf[0],3);
        FileClose(Handle);
    end;