Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

delte part of a file

mossmoss Member Posts: 58
how can I delete some data from the middle of a file?
thanks

Comments

  • Mark StrijbosMark Strijbos Member Posts: 82
    : how can I delete some data from the middle of a file?

    [1] create a temporary file
    [2] write the first part of your original file to the temp file
    [3] skip the part you want to delete
    [4] write the last part of original to the temp file
    [5] copy the temp file "over" your original


  • Eryto MilanskiEryto Milanski Member Posts: 31
    use this
    unit FileW;

    interface

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

    type
    TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    Label2: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Label3: TLabel;
    Button2: TButton;
    Label4: TLabel;
    OpenDialog1: TOpenDialog;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Edit1Change(Sender: TObject);
    procedure Edit2Change(Sender: TObject);
    private
    { Private declarations }
    public
    { Public declarations }
    end;

    var
    Form1: TForm1;
    f,g: TFileStream;
    l: longint;
    FileName : string;
    FlagEdit1 : boolean = false;
    FlagEdit2 : boolean = false;
    FlagOpen : boolean = false;
    implementation

    {$R *.DFM}

    procedure TForm1.Button1Click(Sender: TObject);
    var bFailIfExists : bool;
    begin
    Button1.Enabled := True;
    f:=NIL;
    g:=NIL;

    try
    f:=TFileStream.Create(FileName,fmOpenRead);
    g:=TFileStream.Create('TempFile.fwr',fmCreate);
    if (StrToInt(Edit1.Text)+StrToInt(Edit2.Text)) > f.size
    then
    begin
    ShowMessage('How many bytes you want to remove?!');
    Edit1.SetFocus;
    Edit1.SelectAll;
    exit;
    end;
    g.copyfrom(f,StrToInt(Edit2.Text));
    l := f.seek(StrToInt(Edit1.Text),soFromCurrent);
    l := f.size - l;
    g.copyfrom(f,l);
    MessageDlg(Edit1.Text+' bytes from position '+Edit2.Text+' was removed successful from '+FileName,mtInformation,[mbOK],0);
    finally
    f.free;
    g.free;
    end;
    DeleteFile(FileName);
    CopyFile('TempFile.fwr', PChar(FileName),bFailIfExists);
    DeleteFile('TempFile.fwr');
    Button1.Enabled := False;

    end;

    procedure TForm1.Button2Click(Sender: TObject);
    begin
    if OpenDialog1.Execute then
    begin
    FileName := OpenDialog1.FileName;
    FlagOpen := True;
    if FlagEdit1 and FlagEdit2 then
    Button1.Enabled := True;
    end;
    end;

    procedure TForm1.Edit1Change(Sender: TObject);
    var i : integer;
    begin
    for i:=1 to Length(Edit1.Text) do
    if not (Edit1.Text[i] in ['0'..'9']) then begin
    ShowMessage('The number of bytes must be an integer value');
    FlagEdit1 := False; exit;
    end
    else begin
    FlagEdit1 := True;
    if FlagEdit2 and FlagOpen then Button1.Enabled := True;
    end;
    end;

    procedure TForm1.Edit2Change(Sender: TObject);
    var i : integer;
    begin
    for i:=1 to Length(Edit2.Text) do
    if not (Edit2.Text[i] in ['0'..'9']) then
    begin
    ShowMessage('The position must have an integer value');
    FlagEdit2 := False; exit;
    end
    else begin
    FlagEdit2 := True;
    if FlagEdit1 and FlagOpen then Button1.Enabled := True;
    end;
    end;

    end.



  • Justin BibJustin Bib USAMember Posts: 0

    _________ // http://forcoder.org // free video tutorials and ebooks about // Objective-C, PL/SQL, Visual Basic, C#, Assembly, Python, C, Swift, C++, Scratch, JavaScript, Delphi, Visual Basic .NET, Java, PHP, R, Ruby, Perl, MATLAB, Go Bash, Lua, Prolog, F#, Crystal, Hack, Erlang, FoxPro, Rust, Alice, Julia, Scala, Transact-SQL, VBScript, D, Awk, Kotlin, Lisp, Logo, Apex, ML, COBOL, Fortran, ABAP, SAS, Ada, Dart, LabVIEW, Clojure, Scheme // ___________

Sign In or Register to comment.