whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

40 Visitors Online


 
...check if a file is a text file or a binary one?
Autor: Marcelo Castro
Homepage: http://www.mcgsoftwares.com
[ Print tip ]  

Tip Rating (26):  
     


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;

(* ----- Sample call ----- *)

procedure TForm1.Button1Click(Sender: TObject);
begin
  if 
OpenDialog1.Execute then
  begin
  if 
IsTextFile(OpenDialog1.FileName) then
  
showmessage('is ascii')
  else showmessage('is BinaryFile')
  end;

end;


 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners