I want to check if a file is a plain-text file. I tried the code below:
function IsTextFile(const sFile: TFileName): boolean;
//Created By Marcelo Castro - from Brazil
var
oIn: TFileStream;
iRead: Integer;
iMaxRead: Integer;
iData: Byte;
dummy:string;
begin
result:=true;
dummy :='';
oIn := TFileStream.Create(sFile, fmOpenRead or fmShareDenyNone);
try
iMaxRead := 1000; //only text the first 1000 bytes
if iMaxRead > oIn.Size then
iMaxRead := oIn.Size;
for iRead := 1 to iMaxRead do
begin
oIn.Read(iData, 1);
if (idata) > 127 then result:=false;
end;
finally
FreeAndNil(oIn);
end;
end;
This function works pretty well for text files based on ASCII chars. But text files can also include non-English chars. This function returns FALSE for non-English text files.
Is there any way to check if a file is a text file or a binary file?