How to see if a file text or binary?

I need to detect if a file is text or binary, how do i do this??
LVL 1
lfrodriguesAsked:
Who is Participating?
 
JoeBoothConnect With a Mentor Commented:
Depending on the file size, what you could do is read every byte in the file.    Once you hit a non-text byte, you know it is binary.  However,  on a large file, this approach might not be pratical.

const
    LOW_ASCII                  = #32;

function IsBinary(cFileName:string) : boolean;
var
   F     : tFileStream;
  Buf  : array[0..0] of char;
begin
    result := false;
     f := tFileStream.create( cFileName,fmOpenRead);
    while not f.eof do
    begin
           f.read(Buf,1);
          if ( Buf[0] < LOW_ASCII)   and
              (Buf[0] <> #13) and                        // Carriage Return
              (Buf[0]) and #10) and                    // Line feed
              (Buf[0] and #9) then                      // Tab
        begin
              result := true;
             break;
       end;

    end;
    f.destroy;
end;



end;
0
 
MotazCommented:
You can read all the contents of the file. Text files must only contains ascii code 32 .. 127 or 13, and 10 any other ASCII of of this range that mean this is a binarry file.

Motaz
0
 
runebjCommented:
I don't think there are any operatingsystem functions to determine this. The obvious way is to look for the file extension, but I assume that is not a good enough solution.

Look through the file. If you find characters that have an ASCII value of less than 9 (Tab) you probably have a binary file. If you know that all your textfiles are *just* text - and are written in English, you can also look for characters above "z" (ASCII 122) to determine that a file is binary. (The characters above 122 are international characters and drawing characters.) Otherwise - a textfile should contain a CR/LF (ASCII 13/10) every once in a while and it should end with ASCII 26 (End of file marker).
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
MotazCommented:
Try this function:

- Drop a an OpenDialog and a button then write this code in the Button's OnClick event:

procedure TForm1.Button1Click(Sender: TObject);
var
  F: file;
  Buf: array [0 .. 1000] of Byte;
  i, NumRead: Integer;
  IsText: Boolean;

begin
  if OpenDialog1.Execute then
  begin
    IsText:= True;
    AssignFile(F, OpenDialog1.FileName);
    FileMode:= 0;
    Reset(F, 1);
    while not Eof(F) do
    begin
      BlockRead(F, Buf, SizeOf(Buf), NumRead);
      for i:= 0 to NumRead - 1 do
        if  not (Buf[i] in [32..127, 13, 10]) then
        begin
          IsText:= False;
          Break;
        end;
      if IsText then Break;
    end; // while not
    CloseFile(F);
    if IsText then
      ShowMessage('Text file')
    else
      ShowMessage('Binarry file');
  end;
end;


Motaz
0
 
lfrodriguesAuthor Commented:
i have to a
0
 
lfrodriguesAuthor Commented:
I have to acept JoeBooth's anwser since he gave his sugestion first

Thanks JoeBooth  
0
 
MotazCommented:
ok, but my procedure is more fast than the JoeBooths, if you are not sure, try to open large text files and see the difference,

In binarry files the speed will be the same for both answers

Motaz
0
 
cakkCommented:
BUT, chinese text is always >127
0
 
MotazCommented:
ic, I may be wrong
so you can only change this line:


if  not (Buf[i] in [32..127, 13, 10])

to

if  not (Buf[i] in [32..255, 13, 10])


Motaz
0
All Courses

From novice to tech pro — start learning today.