Thursday, May 14, 2020
Managing Ascii (Text) Files From Delphi Code
Simply put, text files contain readable ASCII characters. We can think of working with a text file in Delphi as analogous to playing or recording information on a VCR tape. Although it is possible to make changes to a text file, jump around when processing information or add some data to the file other than at the end, it is advisable to use a text file only when we know that we are working with ordinary text and no such operations are necessary. Text files are considered to represent a sequence of characters formatted into lines, where each line is terminated by an end-of-line marker (a CR/LF combination). The TextFile and the Assign Method To start working with text files you have to link a file on a disk to a file variable in your code - declare a variable of type TextFile and use the AssignFile procedure to associate a file on a disk with a file variable. Reading information From a Text File If we want to read back the content of a file into a string list, just one line of code will do the job. To read information from a file line by line, we must open the file for input by using the Reset procedure. Once a file is reset, we can use ReadLn to read information from a file (reads one line of text from a file then moves to the next line) : After adding one line of text from a file to a memo component SomeTxtFile needs to be closed. This is done by the Close keyword. We can also use Read procedure to read information from a file. Read works just like ReadLn, except it does not move the pointer to the next line. EOF - End Of File Use the EOF function to make sure that you are not trying to read beyond the end of the file. Lets say we want to display the content of the file in message boxes - one line at a time until we get to the end of a file: Note: It is better to use While loop than the Until loop to take into account the (unlikely) possibility that the file exists but does not contain any data. Writing Text to a File The WriteLn is probably the most common way to send individual pieces of information to a file. The following code will read a text from a Memo1 component (line by line) and send it to some newly created text file. Depending on the state of the file provided to the Rewrite procedure it creates a new file (opens the file for output) with the name assigned to SomeTextFile. If a file with the same name already exists it is deleted and a new empty file is created in its place. If SomeTextFile is already open, it is first closed and then re-created. The current file position is set to the beginning of the empty file. Note: Memo1.Lines.SaveToFile(c:\MyTextFile.txt) will do the same. Sometimes well just need to add some text data to the end of an existing file. If this is the case, well call Append to ensure that a file is opened with write-only access with the file pointer positioned at the end of the file. Something like: Be Aware of Exceptions In general, you should always use exception handling when working with files. I/O is full of surprises. Always use CloseFile in a finally block to avoid the possibility of corrupting a users FAT. All the previous examples should be rewritten as follows: Manipulating With Structured Files Delphi has the ability to handle both ASCII files and files that hold binary data. Here are the techniques for working with typed and untyped (binary) files.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.