StrToFile ( cExpression, cFileName [, lAdditive | nFlag])
Posted: Fri May 01, 2020 9:25 pm
Guys,
my FP-DOS doesn´t know the StrToFile() function, but according the VFP help the StrToFile() requirements are:
1. text can be added, no matter if the file is currently in use.
2. Text encoded as Unicode or utf8 can not be added to an existing file.
3. if unicode or utf8 encoding is used the first bytes of the created file must be:
- 'FF FE' (unicode)
- 'EF BB BF' ( UTF8)
otherwise the default ansi encoding is used.
I would like to know if my current implementation gives the same results as the VFP StrToFile() ? i would also like to know which kind of errors VFP throws, e.g. if a invalid filename or a invalid nFlag param value is used.
Note: In the start() function the cPath var currently points to a "D:test" dir, and the files to be examined are:
"unicode.txt"
"utf8.txt"
"Ansi.txt"
regards
Karl-Heinz
my FP-DOS doesn´t know the StrToFile() function, but according the VFP help the StrToFile() requirements are:
1. text can be added, no matter if the file is currently in use.
2. Text encoded as Unicode or utf8 can not be added to an existing file.
3. if unicode or utf8 encoding is used the first bytes of the created file must be:
- 'FF FE' (unicode)
- 'EF BB BF' ( UTF8)
otherwise the default ansi encoding is used.
I would like to know if my current implementation gives the same results as the VFP StrToFile() ? i would also like to know which kind of errors VFP throws, e.g. if a invalid filename or a invalid nFlag param value is used.
Note: In the start() function the cPath var currently points to a "D:test" dir, and the files to be examined are:
"unicode.txt"
"utf8.txt"
"Ansi.txt"
regards
Karl-Heinz
Code: Select all
USING System.Text
USING System.IO
FUNCTION Start() AS VOID
LOCAL cPath, cFile1, cFile2, cFile3 AS STRING
cPath := "D:test"
cFile1 := cPath + "unicode.txt"
cFile2 := cPath + "utf8.txt"
cFile3 := cPath + "Ansi.txt"
? StrToFile( "Content of unicode file starts with: 'FF FE' " + Time() , cFile1 , 2 )
? StrToFile( "Append Text1" , cFile1 , 3 ) // This not allowed !
? StrToFile( "Append Text2" , cFile1 , 5 ) // This not allowed !
?
? StrToFile( "Content of utf-8 file starts with: 'EF BB BF' " + Time() , cFile2 , 4 )
? StrToFile( "Append Text1" , cFile2 , 3 ) // This not allowed !
? StrToFile( "Append Text2" , cFile2 , 5 ) // This not allowed !
?
? StrToFile( "Text Ansi codepage " + Time() , cFile3 )
? StrToFile( "Append Text1" , cFile3 , TRUE )
? StrToFile( "Append Text2" , cFile3 , 1 )
? StrToFile( "Append Text3" , cFile3 , 1 )
?
RETURN
FUNCTION StrToFile(cExpression, cFileName ) AS DWORD
RETURN StrToFile(cExpression, cFileName , 0 )
FUNCTION StrToFile(cExpression AS STRING, cFileName AS STRING , lAdditive AS LOGIC) AS DWORD
RETURN StrToFile(cExpression, cFileName , IIF ( lAdditive , 1 , 0 ) )
FUNCTION StrToFile(cExpression AS STRING, cFileName AS STRING , nFlag AS DWORD ) AS DWORD
LOCAL enc AS System.Text.Encoding
LOCAL lAdditive AS LOGIC
LOCAL dwWritten AS DWORD
IF nFlag == 3 .OR. nFlag > 4
// allowed flags are 0,1,2 or 4
//
// if the param nFlag value is not valid
// What does VFP do ?
RETURN 0
ENDIF
DO CASE
CASE nFlag == 0 .OR. nFlag == 1
enc := RuntimeState.WinEncoding
IF nFlag == 1
lAdditive := TRUE
ENDIF
CASE nFlag == 2
// ensures that the first two bytes of the file
// are "FF FE"
enc := System.Text.Encoding.Unicode
CASE nFlag == 4
// ensures that the first three bytes of the file
// are "EF BB BF"
enc := System.Text.Encoding.UTF8
ENDCASE
// The same can be done using a FileStream and a StreamWriter,
// instead of File.AppendAllText() and File.WriteAllText()
// Is there any reason to do it not the way i did ?
IF lAdditive
// If the file doesn´t exist it´s automatically created.
File.AppendAllText( cFileName, cExpression + Environment.NewLine, enc )
dwWritten := (DWORD) cExpression:Length
ELSE
// This depends on the SET SAFETY setting
IF File.Exists( cFileName )
// File already exists, ask the user to overwrite the file.
// What does VFP do ?
// RETURN 0
ENDIF
// if the file already exists, it will be overwritten.
File.WriteAllText(cFileName , cExpression + Environment.NewLine , enc )
dwWritten := (DWORD) cExpression:Length
ENDIF
RETURN dwWritten