Detecting 'text' file type (ANSI vs UTF-8)

前端 未结 5 969
北荒
北荒 2020-12-14 23:01

I wrote an application (a psychological testing exam) in Delphi (7) which creates a standard text file - ie the file is of type ANSI.

Someone has ported the program

5条回答
  •  甜味超标
    2020-12-14 23:38

    If the UTF file begins with the UTF-8 Byte-Order Mark (BOM), this is easy:

    function UTF8FileBOM(const FileName: string): boolean;
    var
      txt: file;
      bytes: array[0..2] of byte;
      amt: integer;
    begin
    
      FileMode := fmOpenRead;
      AssignFile(txt, FileName);
      Reset(txt, 1);
    
      try
        BlockRead(txt, bytes, 3, amt);
        result := (amt=3) and (bytes[0] = $EF) and (bytes[1] = $BB) and (bytes[2] = $BF);
      finally    
        CloseFile(txt);
      end;
    
    end;
    

    Otherwise, it is much more difficult.

提交回复
热议问题