FILE_Save

<< Click to Display Table of Contents >>

Navigation:  ThinBASIC Modules > File > FILE: functions working on files >

FILE_Save

 

Description

 

Write a StringBuffer to a file replacing its content.

 

Syntax

 

n = FILE_Save(FileName [Encoding lEncodingType], StringBuffer [, nAttribute])

 

Returns

 

Number.

0 means no errors.

 

Parameters

 

Name

Type

Optional

Meaning

FileName

String

No

Full path name of the file to be created

 

Optionally specify Encoding option followed by encoding type. Use one of the following equates:

%FILE_SAVE_ANSI
StringBuffer will be analyzed:
If it is an UTF8 string, it will be converted into ANSI
If it is an UTF16 Unicode  string, it will be converted into UTF8 and then into ANSI
 

%FILE_SAVE_UTF8
StringBuffer will be analyzed.
If it is an ANSI string, it will be converted into UTF8
If it is an UTF16 Unicode string, it will be converted into UTF8
 

%FILE_SAVE_UTF16
StringBuffer will be analyzed.
If it is an ANSI string, it will be converted into UTF8 and then into UTF16 Unicode
If it is an UTF8 string, it will be converted into UTF16 Unicode
 

 

If no Encoding option is specified, StringBuffer will be written as is.

StringBuffer

String

No

String to put into file

nAttribute

Numeric

Yes

The attributes numeric flag.

Possible values to use for nAttribute:

%FILE_NORMAL

%FILE_READONLY

%FILE_HIDDEN

%FILE_SYSTEM

%FILE_ARCHIVE

 

Use OR operator to set multiple values like:

%FILE_SYSTEM OR %FILE_HIDDEN

 

Remarks

 

If FileName already exists, FileName will be deleted before writing StringBuffer

 

Restrictions

 

If FileName is a directory path, nothing will be saved.

 

See also

 

File Module,

 

Examples

 

Thanks to Abraxas for the following script example

USES "FILE"

 

Dim FileHandle As DWORD

Dim Status     As DWORD

Dim TestString As String

Dim FileName   As String 

Dim sMsg       As String

 

TestString = "1234567890ABCDEF"           ' String to write to file

FileName   = APP_SCRIPTPATH + "test.txt"  ' Build filename

'

' Here are two methods of saving the test string to a file

'

' First method

'

' Limitations - it will only write what is in the SringBuffer

' Advantages - Handles opening/closing the file

'

Status     = FILE_SAVE(FileName, TestString)   ' Save string to File

'

' Second method

'

' Limitations - None (that i can think of)

' Advantages - You can add and remove data

'

' The seek function can be used to move around the file to set the read / write position

'

FileHandle = FILE_Open (FileName, "BINARY")     ' Open file for writing

Status     = FILE_PUT  (FileHandle, TestString) ' Write String to File

Status     = FILE_Seek (FileHandle,  5)         ' Set the current write position to 5th byte in the file

Status     = FILE_PUT  (FileHandle, TestString) ' Write String to File

Status     = FILE_Close(FileHandle)             ' Release File

'

' Now lets try and read some bytes back into memory

' Lets build the string '166'

'

sMsg        = "Bytes read from file are " + $DQ

FileHandle  = FILE_Open(FileName, "BINARY"' Open file for reading

Status      = FILE_Seek(FileHandle,  1)     ' Set the current read position to 1st byte in the file

sMsg       += FILE_Get (FileHandle,  1)     ' Read 1 byte and add to output message

Status      = FILE_Seek(FileHandle, 10)     ' Reset the current File read position to 10th byte in the file

sMsg       += FILE_Get (FileHandle,  1)     ' Read 1 byte and add to output message

Status      = FILE_Seek(FileHandle, 10)     ' File read position has moved to the next byte so we need to Reset to 10th byte in the file

sMsg       += FILE_Get (FileHandle,  1)     ' Read 1 byte and add to output message

Status      = FILE_Close(FileHandle)        ' Release File

 

sMsg       += $DQ + $CRLF + $CRLF

sMsg       += "File Contents " + $DQ

sMsg       += FILE_LOAD(FileName)           ' Read File into Ouput string buffer

sMsg       += $DQ

 

MSGBOX 0, sMsg   ' Output string buffer