From Clomosy Docs
In Clomosy, text files are used for defining, storing text-based data, or performing read/write operations. Text files typically contain data in formats that are readable and editable by humans.
To work with text files in the Clomosy platform, variables of the `TextFile` type are used. There are two types of file operations in the Clomosy platform: standard file operations and customized file operations.
Standard File Operations
Type | Name | Definition Using |
---|---|---|
procedure | AssignFile | Assigns a file name to a file variable. |
function | clFileExists | Checks whether a file exists at the specified file path. |
procedure | ReWrite | The file is opened in write mode; this erases the content of the file and prepares it to write new data. |
procedure | Append | Appends new data to an existing file. |
procedure | WriteLn | Writes a line to the file. |
procedure | CloseFile | Closes the open file. |
procedure | Reset | Opens the file in read or write mode. |
procedure | ReadLn | Reads a line from the file. |
function | EOF | Checks whether the end of the given file's content has been reached. |
function | ClPathCombine | This function returns a string data by concatenating the file path and file name. |
function | ClSaveToFile | It performs the operation of saving a file to the specified file path. |
function | ClLoadFromFile | This function is used to read text or data from a file. |
Example
var file1 : TextFile; { if clFileExists('Test.txt') { ShowMessage('There is a file. Make additions to it.'); AssignFile(file1, 'Test.txt'); Append(file1); WriteLn(file1, 'new line'); WriteLn(file1, 'new line 2'); } else { ShowMessage('There is no file. Make new additions.'); AssignFile(file1, 'Test.txt'); Rewrite(file1); WriteLn(file1, 'new line 1'); } CloseFile(file1); }
Customized File Operations
In the TRObject programming language, the `TclStringList` class provides a list containing text data.
It is used to easily write this data to a file or read from a file.
To explore the usage of `TclStringList` in detail, visit its page.
First, a `TclStringList` object is created. This object represents the list that will hold the text data.
var strList: TclStringList; { strList = Clomosy.StringListNew; }
LoadFromFile
It loads the content of a file into a `TclStringList` object variable by reading from the specified file name.
This procedure reads the entire content of the file and assigns it to a `TclStringList` variable.
While using this method, the file should be checked. If the file is saved at the desired path, it should be loaded into the `TclStringList` object.
procedure LoadFromFile(const FileName: string; Encoding: TEncoding)
This procedure takes two parameters:
- The first parameter specifies the file path to be added.
- The second parameter, Encoding, is used to specify the file's encoding type.
When using the `Encoding` parameter to read the content of the file, you must ensure that the file's encoding type is correctly specified; otherwise, an error will occur.
Use of:
strList.LoadFromFile(Clomosy.AppFilesPath+'MyFile.Txt', 0);
SaveToFile
The data in the `TclStringList` object is written to a file using the `SaveToFile` method.
At this stage, it is important to specify the full path and name of the file.
If the file exists at the desired path, it will be deleted and overwritten.
procedure SaveToFile(const FileName: string; Encoding: TEncoding)
Use of:
strList.SaveToFile(Clomosy.AppFilesPath+'MyFile.Txt', 0);
Example
In the directory where the Clomosy application is located, if the specified file exists, it will be overwritten; if not, the file will be created and data will be written into it.
var strList: TclStringList; fileStr: String; jsonQuery: TclJsonQuery; { strList = Clomosy.StringListNew; jsonQuery = TclJsonQuery.Create(nil); fileStr = clPathCombine('Text.Txt', Clomosy.AppBasePath); try if clFileExists('Text.Txt',Clomosy.AppBasePath) { ShowMessage('File exists.'); strList.LoadFromFile(fileStr,0); ShowMessage(strList.Text); } else { ShowMessage('The file is created and data is added.'); strList.Add('[{"name":"John","age":8},{"name":"Emila","age":80}]'); strList.SaveToFile(fileStr, 0); } if not(strList.Text == '') { jsonQuery = Clomosy.ClDataSetFromJSON(strList.Text); ShowMessage(jsonQuery.getJSONString); } finally strList.Free; jsonQuery.Free; } }