Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   leer archivo txt separado por comas (https://www.clubdelphi.com/foros/showthread.php?t=32720)

fabriciof 14-06-2006 17:56:50

leer archivo txt separado por comas
 
Hola estimados amigos tengo un poblema, quisiera saber como puedo leer un archivo txt separdo por comas de manera que con readln pueda leer cada campo y no asi la fila completa pues necesito rescatar esa informacion en un stringrid

gracias por su ayuda

Yosuun 14-06-2006 18:46:38

Hola yo lo hago de la siguiente manera te pongo el codigo e quitado partes que no son relevantes espero no haber metido la pata ;) y no haber quitado algo importante.

Código Delphi [-]
 
Procedure TWPrograma.TratarFicheroTexto;
Var
  SecFile:TextFile;
  Pf,Pi,Nc:Integer;
  Linea:String;
  Contador:Integer;
  NombreFichero:String;
  C1,C2,C3,C4,C5,C6,C7,C8,C9:String;
  Campo:String;
  PosLp:Integer;
Begin
  AssignFile(SecFile,IFile.FileName);
  Reset(SecFile);
  While not Eof(SecFile) Do
     Begin
       ReadLn(SecFile,Linea);
       Nc:=1;Pi:=1;Campo:='';
       C1:='';C2:='';C3:='';C4:='';C5:='';C6:='';C7:='';C8:='';C9:='';
       while  Pos(',', Linea) > 0 do
          Begin
            IF Pos(',',Linea)>0 Then Pf:=Pos(',',Linea)
            If Nc=1 Then C1:=Copy(Linea,1,Pf-1);
            If Nc=2 Then C2:=Copy(Linea,1,Pf-1);
            If Nc=3 Then C3:=Copy(Linea,1,Pf-1);
            If Nc=4 Then C4:=Copy(Linea,1,Pf-1);
            If Nc=5 Then C5:=Copy(Linea,1,Pf-1);
            If Nc=6 Then C6:=Copy(Linea,1,Pf-1);
            If Nc=7 Then C7:=Copy(Linea,1,Pf-1);
            if Nc=8 then C8:=Copy(Linea,1,Pf-1);
            Linea:=Copy(LInea,Pf,Length(Linea));
            Linea:=Trim(Linea);
            Inc(Nc);
          End;
       C7:=Linea;
       LC1.Caption:=C1;
       LC2.Caption:=C2;
       LC3.Caption:=C3;
       LC4.Caption:=C4;
       LC5.Caption:=C5;
       LC6.Caption:=C6;
       LC7.Caption:=C7;
     End;
  CloseFile(SecFile);
End;


Espero que te sea util.
Un Saludo.

Lepe 14-06-2006 19:10:06

¿Por qué no quieres leer la línea completa? ¿hablamos de varios Gigabytes?

La forma más comoda, es cargar todo el archivo en un TStringList llamado "Archivo", despues accedes a una línea de texto y la descompones en otro StringList llamado Linea:
Código Delphi [-]
var Archivo, Linea:TStringList;
      i:integer;
begin

Archivo := TStringList.Create;
Linea := TStringList.Create;

Archivo.LoadFromFile ('c:\....');
// Accedo  a la linea 4 (empieza en cero) y hago un traspaso de la linea:

Linea.Commatext := Archivo[3]; // separo cada valor de los campos
for i:= 0 to Linea.Count-1 do
 ShowMessage(linea[i]);

// si solo quieres mostrar el 2º campo, pues 
if Linea.Count > 0 then
  ShowMessage(Linea[1]);

Saludos


La franja horaria es GMT +2. Ahora son las 14:42:22.

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