使用搜索和搜索文件

时间:2014-06-05 10:41:47

标签: file delphi rewrite record seek

所以我编写了这段代码,允许用户搜索具有特定名称的文本文件,然后输入文本文件中的第一个名称,并实际编辑该行中写的内容。即搜索名为lions的文本文件,然后编辑名称等于Michael的行。 唯一的问题是,当它被编辑时,名称只会写出之前的内容。我的意思是,如果原始名字是Alexander,然后我编辑该记录以将该名称更改为mike,则文本文件将保留其余字母,以便文本文件中的名称为Mikeander。它保留了剩余的字母。这种情况发生在记录中的所有条目中。

type
    TCustomer = Record
      FirstName : String[15];
      LastName : String[15];
      EventDate : String[15];
      SeatNumber : String[100];
      PhoneNumber : String [100];
      Adress : String[100]
    end;

var
Form4 : TForm4;
FirstName : string;
LastName : String;
EventDate : String;
SeatNumber : String;
PhoneNumber : String;
Adress : String; 




 begin
            usersFilename := Inputbox('Which event needs its details edited?', '', '');
            oldname := inputbox('Old Name','Input old name','');
            newname := inputbox('New Name','Input new name','');

            AssignFile(myFile, usersFilename);
            Reset(myFile);

            //Find the right record position by searching the file for the namne
            count := 0;
            while not Eof(myFile) do
            begin
              Read(myFile, Customer);
              if Customer.FirstName = oldname then
                recordNumber := count;
              count := count+1;
            end;

            //Seek finds that record in the file and points at it, the read will read the current record
            Seek(myFile, recordNumber) ;
            Read(myFile,Customer) ;
            //Set the new name for that record into our customer record
            Customer.FirstName := newName;
            Customer.LastName := Inputbox('Last Name', '', '');
            Customer.EventDate := Inputbox('Event Date', '', '');
            Customer.SeatNumber := Inputbox('Seat Number', '', '');
            Customer.PhoneNumber := Inputbox('Phone Number', '', '');
            Customer.Adress := Inputbox('Adress', '', '');
            //read moves to the next record, so we have to go back again to the original record, then write
            Seek(myFile, recordNumber);
            //Write the current record which just has the name updated
            Write(myFile, Customer);

            showmessage('Successfully updated name');
            CloseFile(myFile);
        end;

如果你能告诉我如何确保它完全删除原始单词,而不是仅仅将它与新单词混合在一起我会非常感激。 感谢

1 个答案:

答案 0 :(得分:0)

如果你在谈论实际文件中的字节,比如MartynA和Free Consulting都在想,那么我过去常常使用这个程序来清理"在实际内容之后的短字符串内存中的垃圾:

{$IFDEF CPUX64 }
PROCEDURE TidyString(VAR STR : OpenString);
  BEGIN
    IF LENGTH(STR)<>HIGH(STR) THEN FillChar(STR[SUCC(LENGTH(STR))],HIGH(STR)-LENGTH(STR),0)
  END;
{$ELSE }
PROCEDURE TidyString(VAR STR : OpenString); ASSEMBLER; PASCAL;
  ASM
            PUSH    EBX
            PUSH    ESI
            PUSH    EDI

            MOV     EDI,STR
            MOVZX   EBX,BYTE PTR [EDI]
            MOV     EDX,EDI
            LEA     EDI,[EDI+EBX+1]
            MOV     ESI,DWORD PTR STR-4
            CMP     ESI,EBX
            JAE     @OK
            MOV     EBX,ESI
            MOV     EDI,EDX
    @LOOP:  MOV     [EDI],BL
            MOV     AL,[EDI+EBX]
            DEC     EBX
            OR      AL,AL
            JZ      @LOOP
            JMP     @OUT
    @OK:    NEG     EBX
            LEA     ECX,[ESI+EBX]
            XOR     AL,AL
            CLD
            REP     STOSB

    @OUT:   POP     EDI
            POP     ESI
            POP     EBX
  END;
{$ENDIF }

设置后调用TidyString(Customer.FirstName),但保存之前。同样,对于记录中的所有其他字段。

免责声明:仅适用于短字符串,因此不适用于移动编译器。

相关问题