Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > Delphi программирование > Общие вопросы Delphi
Регистрация

Восстановить пароль
Повторная активизация e-mail

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 21.01.2014, 17:17   #1
Konstantin_ua
Не судите строго
Форумчанин
 
Аватар для Konstantin_ua
 
Регистрация: 31.03.2011
Сообщений: 202
По умолчанию как прочитать 7 первых байт файла ?

Есть задача, определить тип файла программно, если его расширения было изменено. нашел код
Код:
var
  MyByf: PСhar;
// резервируем память для переменной работы с файлами
 GetMem(MyBuf, 10);
// проверяем считанные байты....
function CheckSign: string;
var
  res : boolean;
  res2 : string;
begin
  res := true;
  res2 := 'unknown';
  if (ord(MyBuf[0])=$ff) and (ord(MyBuf[1])=$fb) then
    begin res := false; res2 := 'mp3'; end;
  if res then if (ord(MyBuf[0])=$ff) and (ord(MyBuf[1])=$f3) then
    begin res := false; res2 := 'mp3'; end;
  if res then if (ord(MyBuf[0])=$52) and (ord(MyBuf[1])=$49) and
    (ord(MyBuf[2])=$46) and (ord(MyBuf[3])=$46) then
    begin res := false; res2 := 'wav mp3'; end;
  if res then if (ord(MyBuf[0])=$49) and (ord(MyBuf[1])=$44) and
    (ord(MyBuf[2])=$33) then
    begin res := false; res2 := 'mp3'; end;
  if res then if (ord(MyBuf[0])=$4d) and (ord(MyBuf[1])=$54) and
    (ord(MyBuf[2])=$68) and (ord(MyBuf[3])=$64) then
    begin res := false; res2 := 'mid'; end;
  if res then if (ord(MyBuf[0])=$ff) and (ord(MyBuf[1])=$d8) and
    (ord(MyBuf[2])=$ff) then
    begin res := false; res2 := 'jpg'; end;
  if res then if (ord(MyBuf[0])=$47) and (ord(MyBuf[1])=$49) and
    (ord(MyBuf[2])=$46) then
    begin res := false; res2 := 'gif'; end;
  if res then if (ord(MyBuf[0])=$00) and (ord(MyBuf[1])=$00) and
    (ord(MyBuf[2])=$01) then
    begin res := false; res2 := 'mpg mpeg'; end; 
  if res then if (ord(MyBuf[4])=$6d) and (ord(MyBuf[5])=$6f) and
    (ord(MyBuf[6])=$6f) and (ord(MyBuf[7])=$76) then
    begin res := false; res2 := 'mov'; end;
  if res then if (ord(MyBuf[0])=$30) and (ord(MyBuf[1])=$26) and
    (ord(MyBuf[2])=$b2) then
    begin res := false; res2 := 'asf'; end;
  if res then if (ord(MyBuf[0])=$25) and (ord(MyBuf[1])=$50) and
    (ord(MyBuf[2])=$44) then
    begin res := false; res2 := 'pdf'; end;
  if res then if (ord(MyBuf[0])=$ca) and (ord(MyBuf[1])=$fe) and
    (ord(MyBuf[2])=$ba) and (ord(MyBuf[3])=$be) and (ord(MyBuf[4])=$00) then
    begin res := false; res2 := 'class'; end;
  if res then if (ord(MyBuf[0])=$4d) and (ord(MyBuf[1])=$5a) and
    (ord(MyBuf[2])=$90) and (ord(MyBuf[3])=$00) then
    begin res := false; res2 := 'exe'; end;
  if res then if (ord(MyBuf[0])=$52) and (ord(MyBuf[1])=$61) and
    (ord(MyBuf[2])=$71) and (ord(MyBuf[3])=$21) and (ord(MyBuf[4])=$1a) and
    (ord(MyBuf[5])=$07) then
    begin res := false; res2 := 'rar'; end;
  if res then if (ord(MyBuf[0])=$50) and (ord(MyBuf[1])=$4b) and
    (ord(MyBuf[2])=$03) and (ord(MyBuf[3])=$04) and (ord(MyBuf[4])=$1a) and
    (ord(MyBuf[5])=$07) then
    begin res := false; res2 := 'zip'; end;

  Result := res2;
end;
// Очищаем память
FreeMem(MyBuf,10);
как открыть файл и считать его первые 7 байт?
Konstantin_ua вне форума Ответить с цитированием
Старый 21.01.2014, 17:32   #2
doktor255
Заблокирован
 
Регистрация: 31.03.2011
Сообщений: 976
По умолчанию

Код:
  p: pChar;
  F: TFileStream;
begin
  F := TFileStream.Create(Filename, fmOpenRead);
  GetMem(p,7);
  f.Position:=0; 
  F.Read(p^,7);
doktor255 вне форума Ответить с цитированием
Старый 21.01.2014, 17:40   #3
type_Oleg
Старожил
 
Аватар для type_Oleg
 
Регистрация: 02.03.2008
Сообщений: 2,499
По умолчанию

Может быть, лучше во всех if определять только res2:
.. then res2 := ...
А res потом, просто
Код:
 res:=res2 = 'unknown';
type_Oleg на форуме Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
Запись загрузчика на флешку. В первых 512 байт (бут сектор). asmars Assembler - Ассемблер (FASM, MASM, WASM, NASM, GoASM, Gas, RosAsm, HLA) и не рекомендуем TASM 6 18.09.2011 12:14
Перезапись первых байт Tetr1s Assembler - Ассемблер (FASM, MASM, WASM, NASM, GoASM, Gas, RosAsm, HLA) и не рекомендуем TASM 0 03.08.2011 02:20
TServerSocket как прочитать 1 байт? YFred Работа с сетью в Delphi 0 11.05.2010 17:41
прочитать последние х байт в файле!! vitalik007 Общие вопросы Delphi 3 08.12.2007 18:39