[RESUELTO] ¿Cómo puedo leer una línea específica de un TXT?
#1
Escrito 08 noviembre 2009 - 09:05
Prueba1
Prueba2
Prueba3
Prueba4
SI quiero leer sólo la línea "Prueba3" y mostrarlo en un TEdit, ¿cómo lo haría?, porque sólo puedo cargar el archivo completo .
Fleon xD
#2
Escrito 08 noviembre 2009 - 11:44
¿De que modo estás abriendo/leyendo el archivo?
Se podría emplear un TStringList, por ejemplo, y abrirlo con LoadFromFile(). Después mediante su propiedad Strings[] posicionarse en alguna línea determinada y leerla.
Saludos,
#3
Escrito 08 noviembre 2009 - 12:18
var SomeTxtFile : TextFile; j: integer; begin AssignFile(SomeTxtFile, 'c:\MyTextFile.txt') ; Rewrite(SomeTxtFile) ; for j := 0 to (-1 + Memo1.Lines.Count) do WriteLn(SomeTxtFile, Memo1.Lines[j]) ; CloseFile(SomeTxtFile) ; end;
Aquí estoy cargando todo el contenido en un Memo, pero no es lo que deseo, pregunta, antes de empezar, sé que con esto se resuelve manejando con INI, pero, si le cambio la extensión, ¿igual se maneja como INI?.
Fleon xD
#4
Escrito 08 noviembre 2009 - 03:07
http://delphiallimit...de-texto-i.html
#5
Escrito 08 noviembre 2009 - 10:11
No entiendo que quieres hacer, pero si conoces la línea que quieres puedes hacer esto.....
procedure TForm1.Button1Click(Sender: TObject); begin Memo1.Lines.LoadFromFile('C:\Desarrollo\Archivo.txt'); ShowMessage(Memo1.Lines[2]); end;
Salud OS
#6
Escrito 08 noviembre 2009 - 11:01
procedure TForm1.Button1Click(Sender: TObject); var SomeTxtFile : TextFile; NumLinea: integer; Linea: String; begin AssignFile(SomeTxtFile, 'c:\MyTextFile.txt') ; Reset(SomeTxtFile) ; NumLinea := 0; REPEAT Readln(SomeTxtFile,Linea); Inc(NumLinea); UNTIL NumLinea = 3; CloseFile(SomeTxtFile) ; ShowMessage(Linea); end;
Salud OS
#7
Escrito 08 noviembre 2009 - 11:07
procedure TForm1.Button1Click(Sender: TObject); var SomeTxtFile : TextFile; Linea: String; begin AssignFile(SomeTxtFile, 'c:\MyTextFile.txt') ; Reset(SomeTxtFile) ; REPEAT Readln(SomeTxtFile,Linea); UNTIL Copy(Linea,1,7) = 'Prueba3'; CloseFile(SomeTxtFile) ; ShowMessage(Linea); end;
Salud OS
#8
Escrito 08 noviembre 2009 - 11:10
Salud OS
#9
Escrito 09 noviembre 2009 - 07:33
Fleon xD
#10
Escrito 09 noviembre 2009 - 07:50
procedure TForm1.Button1Click(Sender: TObject); var Texto : TStringList; Linea :Integer; Archivo : String; begin Linea := StrToInt(Edit1.Text); //Archivo := Asigna aquí tu archivo; Texto := TStringList.Create; try Texto.LoadFromFile(Archivo); if (Linea >=0 )and (Linea <= Texto.Count - 1) then Edit2.Text := Texto.Strings[Linea] finally Texto.Free end; end;
#11
Escrito 09 noviembre 2009 - 08:19
procedure TForm1.Button1Click(Sender: TObject); var I: Integer; Archivo: String; Texto: TStringList; begin // Archivo := Ruta de el archivo Texto := TStringList.Create; try for I := 0 to Texto.Count - 1 do if Texto.Strings[I] = 'Prueba3' then Edit2.Text := Texto.Strings[I] finally Texto.Free end; end;
#12
Escrito 09 noviembre 2009 - 09:28
var File: TextFile; Linea: String; begin AssignFile(File, FileName); Reset(File); repeat ReadLn(File,Linea); Until Copy(Linea,1,5) = 'BAND0'; CloseFile(File); Edit1.Text := Copy(Linea,7,Length(Linea));
Me funciona bien, pero, ¿cómo cargaría 10 líneas diferentes en 10 edits de un sólo tiro?
#13
Escrito 09 noviembre 2009 - 09:37
procedure TForm1.Button1Click(Sender: TObject); var Texto: TStringList; i: Integer; begin Texto := TStringList.Create; Texto.LoadFromFile('C:\Prueba.txt); for i := 0 to Texto.Count -1 do begin if Texto.Strings[i] = 'Prueba3' then Memo2.Lines.Add(Copy(Texto.Strings[i],9,Length(Texto.Strings[i]))); end; Texto.Free; end;
Y no me mostró nada
#14
Escrito 09 noviembre 2009 - 09:46
procedure TForm1.Button1Click(Sender: TObject); var I: Integer; Archivo: String; Texto: TStringList; begin Archivo := 'D:\PP.TXT';// ruta del archivo Texto := TStringList.Create; try Texto.LoadFromFile(Archivo); for I := 0 to Texto.Count - 1 do if Texto.Strings[I] = 'Prueba3' then Memo1.Lines.Add(Texto.Strings[I]) finally Texto.Free end; end;
#15
Escrito 09 noviembre 2009 - 09:55
procedure TForm1.Button1Click(Sender: TObject); var I: Integer; Archivo: String; Texto: TStringList; begin Archivo := 'C:\Prueba.txt';// ruta del archivo Texto := TStringList.Create; try Texto.LoadFromFile(Archivo); for I := 0 to Texto.Count - 1 do if AnsiContainsStr('Prueba3', Texto.Strings[I]) then Memo1.Lines.Add(Texto.Strings[I]) finally Texto.Free end; end;
Y tampoco me funciona , ¿estaré haciendo algo mal?.
#16
Escrito 09 noviembre 2009 - 10:02
#17
Escrito 09 noviembre 2009 - 10:11
¿cómo cargaría 10 líneas diferentes en 10 edits de un sólo tiro?
El siguiente ejemplo carga las primeras 10 líneas de un archivo de texto en 10 TEdits
procedure TForm1.Button1Click(Sender: TObject); var Edits: array [0 .. 9] of TEdit; I: Integer; Archivo: String; Texto: TStringList; begin Edits[0] := Edit1; Edits[1] := Edit2; Edits[2] := Edit3; Edits[3] := Edit4; Edits[4] := Edit5; Edits[5] := Edit6; Edits[6] := Edit7; Edits[7] := Edit8; Edits[8] := Edit9; Edits[9] := Edit10; Archivo := 'D:\PP.TXT';//Ruta del archivo Texto := TStringList.Create; try Texto.LoadFromFile(Archivo); for I := 0 to Length(Edits)-1 do Edits[i].Text := Texto.Strings[I] finally Texto.Free end; end;
#18
Escrito 09 noviembre 2009 - 10:17
De nuevo yo, pos sí estaba haciendo algo mal , no ponía el valor completo de la línea , pero de todos modos, ¿porqué el último code que coloqué no furula?.
El orden de los parámetros de AnsiContainsStr está invertido.
#19
Escrito 09 noviembre 2009 - 10:20
Salud OS
#20
Escrito 09 noviembre 2009 - 10:22
El orden de los parámetros de AnsiContainsStr está invertido.
tremendo, ahora sí furula , gracias amigo, con el último code tuyo y esto ya tengo material suficiente, muchas gracias a todos.
Fleon xD