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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
JoeBoothCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.