PDA

Ver la Versión Completa : Extraer datos de un txt


tiqui_loquito
07-03-2015, 01:13:26
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:

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:

if FicheroCausaExterna[i] =',' then


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

46589,7

Por lo tanto tu comparacion se traduce en

'46589,7' = ',' --> Falso

Aca te dejo mas info para que leas:
1. Delphi al limite: El objeto TStringList (http://delphiallimite.blogspot.com.ar/2007/08/el-objeto-stringlist-i.html)
2. Delphi Basics: TStringList command (http://www.delphibasics.co.uk/RTL.asp?Name=TStringList)

Casimiro Notevi
07-03-2015, 09:52:52
Hola
Recuerda poner los tags al código fuente, ejemplo:

http://www.clubdelphi.com/images/UtilizarTAGs.png

Gracias :)

ecfisa
07-03-2015, 16:39:26
Hola tiqui_loquito.

Si no te entendí mal, te hago esta propuesta:

...
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;


http://s17.postimg.org/qy731nmqn/tiqui_loquito.png

Saludos :)