This pair of functions makes it much easier to move text in and out of files. They don't actually add new capabilities to the language, but they replace large chunks of code to set up files and copy strings in and out of them.
cFileContents = FileToStr( cFileName )
nCharsWritten = StrToFile( cFileContents, cFileName
[ , lAdditive | nFlag ] )
Parameter |
Value |
Meaning |
cFileContents |
Character |
The string read from or written to a text file. |
cFileName |
Character |
The name of the text file to be read or written. |
lAdditive |
.T. |
Add cFileContents to an existing text file. |
.F. |
Overwrite cFileName with cFileContents, if it already exists. If SAFETY is ON and the file exists, the user is prompted before overwriting. |
|
nFlag |
0 |
Overwrite cFileName with cFileContents, if it already exists. If SAFETY is ON and the file exists, the user is prompted before overwriting. |
1 |
Add cFileContents to an existing text file. |
|
2 |
Include the Unicode Byte Order Mark at the beginning of the file. Assumes cFileContents is already in Unicode format. |
|
4 |
Include the UTF-8 Byte Order Mark at the beginning of the file. Assumes cFileContents is already in UTF-8 format. |
|
nCharsWritten |
Numeric |
The number of characters (bytes) written to the text file. |
FileToStr()
reads the data in a file and turns it into a single string, which you can then process using whichever of VFP's string handling tools you prefer. StrToFile()
takes a string you hand it and saves it in a file, giving you a little bit of control over handling overwriting of an existing file.
VFP 7 added the ability to have StrToFile()
create a file in something other than ASCII format. The nFlag parameter lets you use either the Unicode or the UTF-8 character set. Note that this function doesn't convert the provided data into the specified format—it just marks the file as using that format, so that other programs will read it correctly.
Starting in VFP 7, the output file for StrToFile()
is opened shared rather than exclusive. Be careful—this means that it's possible for two different programs to add text to the same file at the same time.
* Here's an unusual way of copying a whole table
cData = FileToStr("MyTable.DBF")
=StrToFile(cData,"MyNewTable.DBF")
[](s4g161.md), \, Low-Level File Functions, TextMerge()