- Method 1: Using the fWriteLine function
- Code samples
- Method 2: Using the fSaveText function
- Principle
- Code samples
How to write into a text or CSV file?
Two methods can be used to write into a text or CSV file: Method 1: Using the fWriteLine function To write a line into a text or CSV file: - Open the file (or create it) with fOpen.
- Write the line with fWriteLine. fWriteLine automatically adds a CR character at the end of line.
- Close the file with fClose.
Code samples - Writing a line:
// Write a simple string nFileNum is int sLineToWrite is string nFileNum = fOpen("MyFile.txt", foCreate) IF nFileNum <> -1 THEN sLineToWrite = "Hello, today is " + DateToString(Today(), "DDDD DD MMMM YYYY") // Write the line fWriteLine(nFileNum, sLineToWrite) fClose(nFileNum) END
- Converting a data file into TXT (export)
<code WL> nFileNum is int sLineToWrite is string nFileNum = fOpen ("ExportProducts.txt", foCreate) IF nFileNum <> -1 THEN FOR EACH Product // The line is made of: // - the product reference // - the product caption // - the product price sLineToWrite = Product.Reference + TAB + Product.ProdCap + TAB + NumToString(Product.Price, "10,2fS") // Write the line fWriteLine(nFileNum, sLineToWrite) END fClose(nFileNum) END
Method 2: Using the fSaveText function Principle To write (create) a text or CSV file in a single operation: - Declare a string variable.
- Write and store all the file lines in memory in this variable.
- Save the content of this variable in a file with fSaveText.
Code samples
// Convert a data file into TXT (export) sLineToWrite is string sFileContent is string FOR EACH Product // The line is made of: // - the product reference // - the product caption // - the product price sLineToWrite = Product.Reference + TAB + Product.ProdCap + TAB + NumToString(Product.Price, "10,2fS") // Write the line in memory sFileContent += sLineToWrite + CR END fSaveText ("ExportProducts.txt", sFileContent)
This page is also available for…
|
|
|
|