Ver Mensaje Individual
  #2  
Antiguo 05-10-2008
Avatar de dec
dec dec is offline
Moderador
 
Registrado: dic 2004
Ubicación: Alcobendas, Madrid, España
Posts: 13.108
Reputación: 34
dec Tiene un aura espectaculardec Tiene un aura espectacular
Hola,

Aún habrá más formas, además de que no esté todo dicho, pero, vamos:

Código Delphi [-]
uses
  SysUtils;

const
  EXAMPLE_FILE_NAME = 'example.txt';
  STRING_TO_COMPARE = 'Content of example.txt';
  STRING_EQUALS_MSG = 'Ok, the string are the same';
  STRING_DIFFER_MSG = 'No, differents strings here';

// Una posible forma leer el archivo y accedera su contenido

procedure TForm1.Button1Click(Sender: TObject);
var
  fs: TFileStream;
  ss: TStringStream;
begin
  fs := TFileStream.Create(
    ExtractFilePath(ParamStr(0))+EXAMPLE_FILE_NAME,
    fmOpenRead
  );
  ss := TStringStream.Create(EmptyStr);
  try
    ss.CopyFrom(fs, fs.Size);
    if(ss.DataString = STRING_TO_COMPARE)then
      ShowMessage(STRING_EQUALS_MSG)
    else
      ShowMessage(STRING_DIFFER_MSG);
  finally
    fs.Free();
    ss.Free();
  end;
end;

// Otra posible forma leer el archivo y accedera su contenido

procedure TForm1.Button2Click(Sender: TObject);
var
  s: string;
  tf: TextFile;
begin
  AssignFile(tf, ExtractFilePath(
    ParamStr(0))+EXAMPLE_FILE_NAME);
  Reset(tf);
  while not Eof(tf) do
    ReadLn(tf, s);
  CloseFile(tf);
  if(s = STRING_TO_COMPARE)then
    ShowMessage(STRING_EQUALS_MSG)
  else
    ShowMessage(STRING_DIFFER_MSG);
end;

// Otra posible forma leer el archivo y accedera su contenido

procedure TForm1.Button3Click(Sender: TObject);
var
  sl: TStringList;
begin
  sl := TStringList.Create();
  try
    sl.LoadFromFile(ExtractFilePath(
      ParamStr(0))+EXAMPLE_FILE_NAME);
    if(Trim(sl.Text) = STRING_TO_COMPARE)then
      ShowMessage(STRING_EQUALS_MSG)
    else
      ShowMessage(STRING_DIFFER_MSG);
  finally
    sl.Free();
  end;
end;
__________________
David Esperalta
www.decsoftutils.com
Responder Con Cita