Function to read and write files to and from disk. Similar to FILETOSTR() and STRTOFILE() but it has additional features useful in busy server applications and certain application scenarios.

Unlike STRTOFILE() and FILETOSTR() in FoxPro, File2Var() is:

  • Multi-user safe and provides for retries if a file is locked
  • Automatically strips UTF-8 and Unicode BOMs from file content

Multi-user safe means that if the file is open for reading, it can still be written to in write mode. Likewise if the file is open in read or write mode you can still read the file.

BOM stripping removes the UTF-8 or Unicode pre-amble that many editors write to identify the document encoding. When reading a file from disk with File2Var() these BOM preambles are stripped off the document so you only receive the actual document content.

o.File2Var(tcFileName, tcString, 
           tlSharedWriteMode, tlWriteAsUtf8)

Return Value

String when reading from file or "" if an error occurs. .T. or .F. when writing a string to file.


File name to retrieve when reading to string. File name to save to when saving to file.

Only applies to writing - string to write to file when writing to file.

Only applies to writing - when .T. the file is written in shared write mode which means if the file is open in another application in shared mode the file will get written anyway. Be careful with this mode flag as it can easily cause confusion between two applications.

Only applies to writing - encodes the file written to UTF8. File is written without a BOM.

See also:

Library wwUtils

© West Wind Technologies, 1996-2024 • Updated: 03/18/2020
Comment or report problem with topic