[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 815
  • Last Modified:

SetFilePointer help

I'm using Delphi's FIleSeek function and am trying to seek to a file's EOF to write to it.
The result isnt static and the result varies. can someone show me how to FileOpen, seek to EOF and append a few lines, afterwards show me how to delete only the lines I just appended to the file.

thanks
Thor371
0
Thor371
Asked:
Thor371
3 Solutions
 
BlackTigerXCommented:
you are mixing text (sequential) files with functions for typed files

if the files are not too big you can get away by loading it to a TStringList and adding/deleting the lines you want there, if you can't do that, then you are using the wrong storage for what you need, a XML file could do a better job, or just a simple database, as I always say, use the right tool for each job
0
 
Slick812Commented:
hello  Thor371, , as near as I can figure from your comments, the next code for a button click, should do what you want done -


procedure TForm1.but_FileOpenClick(Sender: TObject);
var
hFile, len: Integer;
SizeFile: Cardinal;
Str1: String;

  function DoWrite(const InS: String): Boolean;
  begin
  if FileWrite(hFile, InS[1], Length(Str1)) <> Length(InS) then
    begin
    FileClose(hFile);
    ShowMessage('ERROR FileWrite, Disk may be full');
    Result := True;
    end else
    Result := False;
  end;


begin
// button click event
hFile := FileOpen('E:\test.txt', fmOpenReadWrite);
if Cardinal(hFile) = INVALID_HANDLE_VALUE then
  begin
  ShowMessage('ERROR FileOpen, file may NOT exist');
  Exit
  end;

SizeFile := GetFileSize(hFile, nil);

if SizeFile = MAXDWORD then
  begin
  FileClose(hFile);
  ShowMessage('ERROR SizeFile, file may be larger than 4 Gigs');
  Exit;
  end;
FileSeek(hFile,0,2); // end of file

Str1 := 'First New Line'#13#10; // #13#10 for standard notepad line break
if DoWrite(Str1) then Exit;

Str1 := 'Second New Line here'#13#10;
if DoWrite(Str1) then Exit;

Str1 := 'This is new Line 3'#13#10;
if DoWrite(Str1) then Exit;

len := FileSeek(hFile,0,2);
// FileSeek(hFile,0,2) is always accurate when you call it
// so call it many times, not just once .
// always call it just before you use it's return value
SetLength(Str1, len);
FileSeek(hFile,0,0);
if FileRead(hFile, Str1[1], len) <> len then
  begin
  FileClose(hFile);
  ShowMessage('ERROR FileRead, failure of disk access');
  Exit;
  end;
Memo1.Text := Str1;
FileSeek(hFile,SizeFile,0);
SetEndOfFile(hFile);
FileClose(hFile);
end;


 - - - - - - - - - - - - - - - - - - - - - - - -
ask questions if you need more information
0
 
OctabunCommented:
Yes, he/she is mixing, or to be correct wants to be mixing, different types of functions. So what? The following unit does what he/she wants.

unit Unit1;

interface

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

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

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  f: integer;
  s: string;
  d: string;
begin
  f := FileOpen('test.txt', fmOpenReadWrite or fmShareExclusive);
  filePos := FileSeek(f, 0, 2);
  d := #13#10;
  s := 'line added';
  FileWrite(f, s[1], length(s));
  FileWrite(f, d[1], 2);
  s := 'another line added';
  FileWrite(f, s[1], length(s));
  FileWrite(f, d[1], 2);
  s := 'final line added';
  FileWrite(f, s[1], length(s));
  FileWrite(f, d[1], 2);
  FileClose(f);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  f: file of byte;
begin
  AssignFile(f, 'test.txt');
  Reset(f);
  Seek(f, filePos);
  Truncate(f);
  CloseFile(f);
end;

end.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now