[SOLVED ]Detect if a file is a typed file lazarus / 21-General ---------------------------------------------------------------------- torbente · 2021-01-04 06:22 Hello; i have the following Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Mydata= Packed Record     Data1: string[15];     Data2: int64;     Data3: boolean;     end;   myfile: file of mydata; How i can detect if a file is of this type when i open it? I used this approach, but txt files are opened anyway Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i assignfile(myfile,Filename);tryreset(myfile)...except on E:Exception do   ...end; ---------------------------------------------------------------------- Peter H · 2021-01-04 08:07 I can only think about a heuristic approach. Data1 must probably be plain text. Data3 must be 0 or 1. This must be true for all records in the file. Filesize must be a multiple of the record size. It would be better to start the file with a magic number, maybe a special record that has unique content. ---------------------------------------------------------------------- ASerge · 2021-01-04 09:18 Quote from: torbente on January 04, 2021, 07:22:48 amHow i can detect if a file is of this type when i open it? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$MODE OBJFPC}{$APPTYPE CONSOLE}{$LONGSTRINGS ON} uses SysUtils; type  TMyData = packed record    Data1: string[15];    Data2: Int64;    Data3: Boolean8;  end;   TMyDataFile = file of TMyData; const  CItemsToStoreInFile = 10; var  CHeader: TMyData = (Data1: 'MyFileSignature'; Data2: $8BADF00DDEFEC8ED; Data3: False); procedure MakeDataFile(const FileName: string);var  F: TMyDataFile;  R: TMyData;  i: SizeInt;begin  AssignFile(F, FileName);  Rewrite(F);  try    Write(F, CHeader);    R.Data1 := 'Data1';    R.Data2 := 2;    R.Data3 := True;    for i := 1 to CItemsToStoreInFile - 1 do      Write(F, R);  finally    CloseFile(F);  end;end; procedure MakeTextFile(const FileName: string; FileSize: SizeInt);var  F: TextFile;  C: AnsiChar;  i: SizeInt;begin  AssignFile(F, FileName);  Rewrite(F);  try    for i := 1 to FileSize do    begin      C := Chr(Random(64) + Ord(' '));      Write(F, C);    end;  finally    CloseFile(F);  end;end; function TextFileIsOpenedAsDataFile(const FileName: string; CheckHeader: Boolean): Boolean;var  F: TMyDataFile;  R: TMyData;begin  AssignFile(F, FileName);  try    Reset(F);    try    Result := True;      if CheckHeader then      begin        Read(F, R);        Result := (R.Data1 = CHeader.Data1) and (R.Data2 = CHeader.Data2) and          (R.Data3 = CHeader.Data3);      end;    finally      Close(F);    end;  except    on E: Exception do    begin      Writeln('Error: ', E.Message);      Result := False;    end;  end;end; const  CDataFileName = 'file.data';  CTextFileName = 'file.txt'; begin  MakeDataFile(CDataFileName);  MakeTextFile(CTextFileName, SizeOf(TMyData) * CItemsToStoreInFile);  if TextFileIsOpenedAsDataFile(CTextFileName, False) then    Writeln('It is possible')  else    Writeln('Strange');  if TextFileIsOpenedAsDataFile(CTextFileName, True) then    Writeln('Very strange')  else    Writeln('Yes!');  Readln;end. ---------------------------------------------------------------------- Warfley · 2021-01-04 13:45 Well, you can't, bytes are bytes. Besides a simple heuristic as suggested above, you could check if the filesize is a multiple of the record size but thats about it. Usually thats why most custom filetypes have a header containing custom information like a magic number. The reading of the file is than as follows: 1. Read the header 2. Check if the magic number matches 3. check additional header information for parsing (e.g. version number) 4.a. If 2 and 3 do not match what is expected (unknown version number, wrong magic number), throw an error 4.b. Otherwise read file in the format indicated by the header ---------------------------------------------------------------------- Bart · 2021-01-04 14:03 With one of my old applications I used the following approach: The filesize must be N times the sizeof(recordstructure), where N is an integer.Like ASerge suggested, the firs record I write to that file was a special record. Just check for that if you read the file. In the text field of the header record, you can put some special characters like #0, ^D and ^Z. This has the advantage that when you do "type filename" on the commandprompt, it will stop displaying information after it encounterd those characters: ^Z is EOF for text files, ^D maybe has the same meaning on *nix (I'm not really sure). It also prevents writing such a header using a simple text-editor (notpead and the like). Bart ---------------------------------------------------------------------- torbente · 2021-01-05 16:45 Thanks. I will use the magic number method.