[function]將WideString寫入unicode檔案的函數

有關Delphi 的語法, 程式, 等
回覆文章
頭像
tim
文章: 1379
註冊時間: 2008年 11月 26日, 00:49

[function]將WideString寫入unicode檔案的函數

文章 tim »

利用 TFileStream 配合 WideString 寫入成 unicode 的 littel endian 的文字檔

代碼: 選擇全部

     
    function WriteWideString2File(filename: string; ws: WideString):Boolean; 
    const 
      WideBOM_LSB_First      = WideChar(#$FEFF); 
    var 
      wf: WideString; 
      f: TFileStream; 
      i: integer; 
    begin 
      Result := false; 
      wf := WideBOM_LSB_First; 
      f:= TFileStream.Create(filename, fmCreate); 
      try 
        f.Write(wf[1], 2); 
        //for i:= 1 to length(ws) do 
        //  f.Write(ws[i], sizeof(ws[i])); 
        f.Write(PWideChar(ws)^, length(ws)*sizeof(WideChar)); 
        Result := true; 
      finally 
        f.Free; 
      end; 
    end; 

參考文章: http://delphi.ktop.com.tw/topic.asp?TOPIC_ID=46228
多多留言, 整理文章, 把經驗累積下來.....
回覆文章