Delete record file delphi




















It often makes sense to use a linked list in a data file, especially if the file is too big to rewrite quickly. If you need sample code to illustrate this concept, let me know. Well, if you delete record 47 and shift all the following records down , you'd need to Seek to 48, Read it, Seek to 47, Write the read record, Seek to 49, Read it, Seek to 48, Write, etc.

This also implies that the process of read the file look at every record and determine with the flag if it "exists" or not. This technique is faster than above, but the file size continually grows as the application perpetuates.

It also has the advantage that a new record can be written to the locations of "dead records", rather than appended and increasing the size. A disadvantage is that any "order" of the file isn't maintained unless dead record space isn't reused. Another technique is to maintain a separate file or a portion of the data file which contains an "index" of valid records, possibly sorted in "key" order, but which identifies the actual file offset of each existing record.

Perhaps you can see that this explanation isn't full, but you could work from it. Still another way is to design the file on a key "hash" where every record has a unique key which is used to compute a physical record location for each record is stored.

This is very fast record acces and manipulation, but it should allow for some "overflow" mechanism for the occasional situations where two or more keys hash to the same location.

However, you can use the Seek function to move to any position in the file. For instance, let's say you want to move to record 51 in a file. To do that, you'd make a call similar to the following:. From there you could read or write a file variable depending on the prescribed action.

Each read or write operation will always position the file pointer at the record following the record that received the action. For example, if the record your program just read was record 2, immediately following the call the file pointer would be placed on record 3.

This positioning isn't much of an issue for sequential writes to a file; however, it's a serious issue for sequential reads. Were you to read the last record of a file, then immediately attempt to read the next record which is nonexistent , you'd get a runtime error because you've attempted to read beyond the end of the file.

As luck would have it, though, you can call on a useful routine named FilePos to determine your position in the file. FilePos is a function that returns the current record in a file of records. If you're doing sequential reads in a file, you'll want to make use of this function and compare its value against the FileSize function, which returns the total count of records in the file. But since the last record in a file is always numbered as one less than the total number of records, you must always subtract one from the FileSize function to safely make a comparison between your current position and the end of the file.

Below is the skeleton of a looping structure that illustrates positional comparison:. Now that we've covered the basics of opening and closing a file, and determining and manipulating our position in a file of records, we're ready to discuss the editing functions--or more simply, reading and writing to a file.

To read a record from a file of records, you simply call the Read procedure and specify file and record variables as formal parameters such as. Pretty simple stuff. Similarly, to write a record into a file of records, you call the Write procedure and provide the same formal parameters as in the Read procedure:. It's possible to read several records from and write several records to a file of records at one time by overloading the record parameter.

For instance, let's say you want to read three records from a file simultaneously. In that case, you'd do something like the following:.

One thing you must keep in mind when using these functions: The file pointer is always positioned at the next record following the call.

I mentioned earlier that this is a serious consideration when you're performing sequential reads from a file of records, but it can also be serious when you're editing a record.

For instance, let's say you want to read record 5 from the file, edit it, then write it back to file. The proper way to write back to the original record would be as follows:. As you can see, I use an extra Seek immediately before the Write to move the file pointer back to the proper position. Strangely enough, both novices and experts make this fairly common error. The most important thing to remember regarding manipulating files of records is the fate of the file pointer position in relation to a Read or a Write operation.

We've just covered a lot of ground, so let's quickly review the various functions discussed above. Table A lists the functions and the resulting actions. Truncate Deletes the remainder of a file from a given position As you can see, you need to know only a handful of routines in order to manipulate files of records. With this information, you can now write applications that revolve around the concepts we've explored. Before you begin, though, notice that I made no mention of the record structure with respect to any of the functions above.

That's the beauty of these routines! They're so generalized that all you have to do is supply the right type of file variable, and the functions do all the work. These functions don't care what your record structure is, so long as your file variable points to a file that's of the correct type.

Pretty nifty. In any case, play around with this stuff. Once you get the hang of working with files of records, you'll probably use them more often. You move data back and forth between the database and the user interface i. This buffer is necessary because, unlike data-aware components, there's no such thing as a data link that will link TEdit objects to fields in a record. The ReadRec function reads the record at the current position in the file into a global record variable named AddrRec of type TAddressRec and then writes the variable's fields to the appropriate TEdit components' Text properties.

However, you'll use the individual write methods to write data to the database, since these methods are position-dependent. Updating a record to the database merely involves moving the data from the TEdit components to the temporary buffer variable, then writing the record variable's contents to the file. However, no such thing as a field-level update exists when you're working with a file of records.

When you update a record at a particular position within the file, you're actually completely overwriting the record at that position. In fact, the mechanics for adding a new record to the end of the database and overwriting an existing record are nearly identical. The only difference is that when appending a record to the database, the program merely performs a Seek beyond the end of the file, then writes the record variable.

I mentioned earlier that it's impossible to insert or delete a record in a particular position in a file of records. It's true that Delphi offers no functions that deal with these actions directly; however, that doesn't mean you can't code for them.

To insert or delete a record in a file of records, you employ a buffer to temporarily hold records that you want to write back to the file. Let's quickly go over the logic of each operation before we examine the code. To insert a new record, you follow these basic steps:. Initialize an array of records of the appropriate record type. Get the current position of the file pointer and save it. Transfer the contents of the TEdit components into a temporary record variable.

Seek to the beginning of the file. Read each record from the beginning of the file up to the record just before the insert position. Refbacks are aus. Seite 1 von 2.

Apr , Delphi-Version: DeddyH Registriert seit: Apr , Why not define a specialized list? Apr , Zitat von DeddyH : Why not define a specialized list? How to do that please? Apr , Can you post your real code? Apr , I still don't understand the problem. Apr , many thanks for your great efforts , is there any other alternative to load just one field say : myRec. Forumregeln Es ist dir nicht erlaubt , neue Themen zu verfassen. HTML-Code ist aus. Trackbacks are an Pingbacks are an Refbacks are aus Foren-Regeln.

Alle Foren als gelesen markieren.



0コメント

  • 1000 / 1000