Ver Mensaje Individual
  #18  
Antiguo 12-07-2013
Avatar de nlsgarcia
[nlsgarcia] nlsgarcia is offline
Miembro Premium
 
Registrado: feb 2007
Ubicación: Caracas, Venezuela
Posts: 2.206
Reputación: 23
nlsgarcia Tiene un aura espectacularnlsgarcia Tiene un aura espectacular
force1758,

Cita:
Empezado por force1758

...cargar de aun archivo de texto(txt), linea por linea...separando cada linea colocare un valor que contenga esa linea..

...necesito es cojer la información de cada linea y colocarle una variable...

...lo que quiero en si es crear una función que lea cada linea del archivo luego separar esas lineas en valores sin utilizar tmemo, ni form , ni edit ya que esta es una DLL la cual yo utilizarlo...

...y utilizar esos valores obtenido en otro procedimientos...

...pero como podría hacer para escojer en el ciclo las lineas una por una...
Te comento:

1- Cualquiera de las opciones sugeridas anteriormente funciona en un DLL.

2- En un DLL se pueden incluir componentes visuales.

Revisa este código:
Código Delphi [-]
library DLLFileProcess;

uses
   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs;

var
   FileVector : Array of String;
   FileInput : String;

{$R *.res}

// Carga un Archivo Línea a Línea en el Arreglo FileVector
function ReadTextFile(FileName : PChar) : Boolean; Stdcall;
var
   F : TextFile;
   i : Integer;
   Buffer : String;

begin

   if FileName = EmptyStr then
   begin
      MessageDlg('El Nombre del Archivo No Puede Estar en Blanco', mtInformation, [mbOK], 0);
      Result := False;
      Exit;
   end;

   if not FileExists(FileName) then
   begin
      MessageDlg('El Archivo no Existe en el Path Indicado', mtInformation, [mbOK], 0);
      Result := False;
      Exit;
   end;

   FileInput := FileName;

   try
      FileMode := fmOpenRead;
      AssignFile(F, FileName);
      Reset(F);
      i := 1;
      while not Eof(F) do
      begin
         Readln(F, Buffer);
         SetLength(FileVector,i);
         FileVector[i-1] := Buffer;
         Inc(i);
      end;
      CloseFile(F);
      Result := True;
   except
      MessageDlg('Error de I/O', mtInformation, [mbOK], 0);
      Result := False;
   end;

end;

// Genera un Archivo de Salida con la Información del Arreglo FileVector
function WriteTextFile() : Boolean; Stdcall;
var
   FileOutput : String;
   F : TextFile;
   i : Integer;
   Buffer : String;

begin

   FileOutput := ExtractFilePath(FileInput) + 'FileOutPut.txt';

   if Length(FileVector) = 0 then
   begin
      MessageDlg('El Arreglo No Contiene Información', mtInformation, [mbOK], 0);
      Result := False;
      Exit;
   end;   

   try
      FileMode := fmOpenWrite;
      AssignFile(F, FileOutput);
      Rewrite(F);
      for i:= low(FileVector) to High(FileVector)  do
      begin
         Buffer := 'LN-' + IntToStr(i+1) + ' ' + FileVector[i];
         Writeln(F, Buffer);
      end;
      CloseFile(F);
      Result := True;
   except
      MessageDlg('Error de I/O', mtInformation, [mbOK], 0);
      Result := False;
   end;

end;

Exports
   ReadTextFile,
   WriteTextFile;
   
begin
end.
El código anterior lee un archivo de texto línea x línea en un DLL, almacena su contenido en un arreglo y genera un nuevo archivo con el arreglo como ejemplo de procesamiento del mismo.

Revisa este código:
Código Delphi [-]
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  function ReadTextFile(FileName : PChar) : Boolean; Stdcall; External 'DLLFileProcess.dll';
  function WriteTextFile() : Boolean; Stdcall; External 'DLLFileProcess.dll';
  
var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
   OpenDialog : TOpenDialog;

begin
   OpenDialog := TOpenDialog.Create(self);
   OpenDialog.InitialDir := 'C:\';
   OpenDialog.Options := [ofFileMustExist];
   OpenDialog.Filter := '*.txt';

   if OpenDialog.Execute then
   begin

      if ReadTextFile(PChar(OpenDialog.FileName)) then
         MessageDlg('Archivo Procesado Exitosamente', mtInformation, [mbOK], 0)
      else
         MessageDlg('Error en Procesamiento de Archivo', mtInformation, [mbOK], 0)

   end
   else
      MessageDlg('No Se Selecciono Ningún Archivo', mtInformation, [mbOK], 0);

   OpenDialog.Free;

end;

procedure TForm1.Button2Click(Sender: TObject);
begin

   if WriteTextFile() then
      MessageDlg('Archivo Generado Exitosamente', mtInformation, [mbOK], 0)
   else
      MessageDlg('Error en Generación de Archivo', mtInformation, [mbOK], 0)

end;

end.
El código anterior permite llamar a las funciones del DLL del ejemplo precedente para el procesamiento de un archivo de texto.

Todo el código comentado esta disponible en el link: http://terawiki.clubdelphi.com/Delph...ileProcess.rar

Te sugiero revisar el ejemplo del link como todos los ejemplos sugeridos anteriormente, de seguro te serán de utilidad en tu proyecto.

Espero sea útil

Nelson.

Última edición por nlsgarcia fecha: 12-07-2013 a las 02:21:02.
Responder Con Cita