Jose Martinez
Jose Martinez

Reputation: 313

Read text files in Delphi

Hi I am having a problem running a function to read a text file the problem seems to be that my antivirus blocks my delphi console program because when I do for a visual form there is no problem .

Tengos two codes one is this :

function LeerArchivox(const filename: TFileName): String;
var
  List: TStringList;
begin

  if (FileExists(filename)) then
  begin

    List := TStringList.Create;
    List.Loadfromfile(filename);
    Result := List.text;
    List.Free;

  end;

end;

This goes to perfection but do not want to use the component Classes for the program does not weigh much .

Also I have this :

function leerarchivo(filealeer: string): string;

var
  abriendo: TextFile;
  lineasleyendo: string;
  finaldearchivo: string;

begin

  finaldearchivo := '';
  AssignFile(abriendo, filealeer);
  Reset(abriendo);

  while not Eof(abriendo) do
  begin
    ReadLn(abriendo, lineasleyendo);
    finaldearchivo := finaldearchivo + lineasleyendo;
  end;

  CloseFile(abriendo);

  Result := finaldearchivo;

end;

Other code.

function leerarchivo3(archivoaleer: string): string;

const
  BUFF_SIZE = $8000;
var
  dwread: LongWord;
  hFile: THandle;
  datafile: array [0 .. BUFF_SIZE - 1] of ansichar;
  codigofinal: string;

begin

  codigofinal := '';

  hFile := CreateFile(PChar(archivoaleer), GENERIC_READ,
    FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING,
    FILE_ATTRIBUTE_READONLY, 0);

  SetFilePointer(hFile, 0, nil, FILE_BEGIN);

  Readfile(hFile, datafile, BUFF_SIZE, dwread, nil);

  while (dwread > 0) do
  begin
    Readfile(hFile, datafile, BUFF_SIZE, dwread, nil);
    codigofinal := codigofinal + datafile;
  end;

  Result := codigofinal;

end;

This is the problem because when I use my antivirus deletes it at the time , my question to other alternatives I have to read a text file without using Classes.

Someone can help me?

Upvotes: 3

Views: 59423

Answers (4)

wahyujana
wahyujana

Reputation: 21

var txt : TextFile; 
begin 
  AssignFile(txt,'filetxt.txt'); 
  ReWrite(filetxt); 
  Writeln(filetxt,memo1.Lines.Text); 
  CloseFile(filetxt); 
  Reset(filetxt); 
  while not Eof(filetxt) do 
  begin 
    Readln(filetxt); 
  end; 
  CloseFile(filetxt);

Upvotes: 2

Ken White
Ken White

Reputation: 125689

This code works fine for me as a console application, Delphi 2007, running on Win7 64:

Contents of 'E:\TempFiles\Test.txt':

One
Two
Three
Four

Source:

program Project2;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  Txt: TextFile;
  s: string;
  AllText: string;

begin
  AllText := '';
  AssignFile(Txt, 'E:\TempFiles\test.txt');
  Reset(Txt);
  while not Eof(Txt) do
  begin
    Readln(Txt, s);
    AllText := AllText + s;

    // Write out each line; comment out to stop.
    Writeln(s);
  end;
  CloseFile(Txt);

  // Write out all content as a single string.
  WriteLn(AllText); 
  ReadLn;
end.

Produces output:

One
Two
Three
Four
OneTwoThreeFour

Upvotes: 13

HpTerm
HpTerm

Reputation: 8281

You can use win32 api.

In one of my apps I do things like that, extend/modify to match your needs. This only use Win32 API and does not lock the file. It's like notepad. When you open a file with notepad it is not locked and can still be written or read by other software.

const
  BUFF_SIZE = $8000;
var
  dwread:LongWord;
  hFile: THandle;
  datafile : array [0..BUFF_SIZE-1] of ansichar;

//create file handler
hFile := createfile(PChar(TFilePanel(FilePanelList.Items[i-1]).LongFileName), GENERIC_READ,
            FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, 0);

//set file pointer to beginning of file
SetFilePointer(hFile, 0, nil, FILE_BEGIN);

//read the file
try
    Readfile(hFile, datafile, BUFF_SIZE, dwread, nil);

    while (dwread > 0) do
    begin
      //read/use datafile     
      Here_do_something_with_datafile;

      Readfile(hFile, datafile, BUFF_SIZE, dwread, nil);
    end;
finally
   closehandle(hFile);
end;

Upvotes: 5

Stijn Sanders
Stijn Sanders

Reputation: 36840

If you don't want to use the Classes unit, only need to operate on this file, and are making a Windows executable, you could use the Windows unit instead and call the Win32 API functions: CreateFile, ReadFile, CloseHandle and related functions.

Upvotes: 0

Related Questions