Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Extraer datos de un txt (https://www.clubdelphi.com/foros/showthread.php?t=87846)

tiqui_loquito 07-03-2015 01:13:26

Extraer datos de un txt
 
Hola a todos,

Creo un txt donde guardo el id del paciente y una causa externa en un .txt, separado por una coma.
Ej: 46589,7

Solo guardo un dato en el archivo, no se guarda mas.

Aun soy nuevo en delphi y quisiera saber como extraer los datos.

Mi código es:
Código Delphi [-]
     FicheroCausaExterna := TStringList.Create; 
     Dir:= ExtractFilePath(application.ExeName)+'CausaExterna.txt'; 
     CausaExterna := Fdm.CausaExterna(Datos.CausaExterna); 
 
     if FileExists(Dir) then 
      FicheroCausaExterna.LoadFromFile(Dir) 
     else 
     begin 
      FicheroCausaExterna.Create; 
      FicheroCausaExterna.Add(IntToStr(Datos.Paciente)+','+CausaExterna); 
      FicheroCausaExterna.SaveToFile(Dir); 
     end; 
 
//Aquí quiero extyrar los datos, peor aun no se como
     for i:=0 to FicheroCausaExterna.Count-1 do 
      if FicheroCausaExterna[i]=',' then 
 
 
    if FicheroCausaExterna.Count > 0 then 
     begin 
      CausaExternaAnterior:= FicheroCausaExterna[0]; 
      CambioCauExterna := (CausaExterna<>CausaExternaAnterior); 
 
      if CambioCauExterna then 
      begin 
        FicheroCausaExterna.Clear; 
        FicheroCausaExterna.Add(CausaExterna); 
        FicheroCausaExterna.SaveToFile(Dir); 
      end 
     end;
Gracias

AgustinOrtu 07-03-2015 07:02:57

Yo te recomendaria hacer un bucle en el que llames a ShowMessage con cada string de tu TStringList, asi te das cuenta como carga tu archivo.

En tu ejemplo, la comparacion:
Código Delphi [-]
  if FicheroCausaExterna[i] =',' then

Siempre te va a dar falso, porque tu archivo tiene una sola linea que es la siguiente:

Cita:

46589,7
Por lo tanto tu comparacion se traduce en

Cita:

'46589,7' = ',' --> Falso
Aca te dejo mas info para que leas:
1. Delphi al limite: El objeto TStringList
2. Delphi Basics: TStringList command

Casimiro Notevi 07-03-2015 09:52:52

Cita:

Empezado por tiqui_loquito (Mensaje 489707)
Hola

Recuerda poner los tags al código fuente, ejemplo:



Gracias :)

ecfisa 07-03-2015 16:39:26

Hola tiqui_loquito.

Si no te entendí mal, te hago esta propuesta:
Código Delphi [-]
...
implementation
var
  IdCe: TStrings; // almacena id y causa externa

procedure TForm1.FormCreate(Sender: TObject);
var
  TS: TStrings;
  i,p: Integer;
begin
  IdCe:= TStringList.Create;
  TS:= TStringList.Create;
  try
    TS.LoadFromFile('CausaExterna.txt');
    for i:= 0 to TS.Count-1 do
    begin
      p:= Pos(',',TS[i]);
      IdCe.AddObject(Copy(TS[i], 1, p - 1),
        TObject(StrToInt(Copy(TS[i], p + 1, MaxInt))));
    end;
  finally
    TS.Free;
  end;
end;

procedure AgregarCausa(const Id, Causa: string);
begin
  IdCe.AddObject(Id, TObject(StrToInt(Causa)));
end;

procedure ModificarCausa(const Id, Causa: string);
var
  ix: Integer;
begin
  ix:= IdCe.IndexOf(Id);
  if ix <> -1 then
    IdCe.Objects[ix]:= TObject(StrToInt(Causa));
end;

procedure BorrarCausa(const Id: string);
var
  ix: Integer;
begin
  ix:= IdCe.IndexOf(Id);
  if ix <> -1 then
    IdCe.Delete(ix);
end;

procedure MostrarDatos(TS: TStrings);
var
  i: Integer;
begin
  for i:= 0 to IdCe.Count-1 do
    TS.Add(IdCe[i] { ID } + ' ' +
           IntToStr(Integer(IdCe.Objects[i])) { causa externa});
end;

procedure TForm1.btnDemoClick(Sender: TObject);
begin
  AgregarCausa('11111','0');     // ejemplo agregar
  MostrarDatos(ListBox1.Items);  // mostrar
  ListBox1.Items.Add('------------------------');
  ModificarCausa('11111','11');  // ejemplo modificar
  MostrarDatos(ListBox1.Items);  // mostrar
  BorrarCausa('11111');          // ejemplo borrar
  ...
end;



Saludos :)


La franja horaria es GMT +2. Ahora son las 18:18:16.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi