Sunday, November 24, 2019
Managing Ascii (Text) Files From Delphi Code
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. var à SomeTxtFile : TextFile; begin à AssignFile(SomeTxtFile, FileName) 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. Memo1.Lines.LoadFromFile(c:\autoexec.bat) 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) : var à SomeTxtFile : TextFile; à buffer : string;begin à AssignFile(SomeTxtFile, c:\autoexec.bat) ; à Reset(SomeTxtFile) ; à ReadLn(SomeTxtFile, buffer) ; à Memo1.Lines.Add(buffer) ; à CloseFile(SomeTxtFile) ; end; 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. var à SomeTxtFile : TextFile; à buf1,buf2 : string[5]; begin à AssignFile(SomeTxtFile, c:\autoexec.bat) ; à Reset(SomeTxtFile) ; à ReadLn(SomeTxtFile, buf1,buf2) ; à ShowMessage(buf1 buf2) ; à CloseFile(SomeTxtFile) ; end; 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: var à SomeTxtFile : TextFile; à buffer : string;begin à AssignFile(SomeTxtFile, c:\autoexec.bat) ; à Reset(SomeTxtFile) ; à while not EOF(SomeTxtFile) do à begin à à ReadLn(SomeTxtFile, buffer) ; à à ShowMessage(buffer) ; à end;à CloseFile(SomeTxtFile) ;end; 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. var à SomeTxtFile : TextFile; à j: integer; begin à AssignFile(SomeTxtFile, c:\MyTextFile.txt) ; à Rewrite(SomeTxtFile) ; à for j : 0 to (-1 Memo1.Lines.Count) do à à à WriteLn(SomeTxtFile, Memo1.Lines[j]) ; à CloseFile(SomeTxtFile) ; end; 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: var à SomeTxtFile : TextFile; begin à AssignFile(SomeTxtFile, c:\MyTextFile.txt) ; à Append(SomeTxtFile) ; à WriteLn(SomeTxtFile, New line in my text file) ;à CloseFile(SomeTxtFile) ;end; 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: var à SomeTxtFile : TextFile; à buffer : string; begin à AssignFile(SomeTxtFile, c:\MyTextFile.txt) ; à try à à Reset(SomeTxtFile) ; à à ReadLn(SomeTxtFile, buffer) ; à finally à à CloseFile(SomeTxtFile) ; à end;end; 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.