Enciding

Top  Previous  Next

Encoding

fblogo_mini

Specifies character format of a text file

 

Syntax

 

Open filename for {InputeOutput|Append} Encoding "utf-8"|"utf-16"|"utt-32"|"ascii" #s [#]filenum

 

Paramaters

 

filename for {Input|Output|Append}

file name to open for Input, Outppt, or Append

Encoding "utt-8"|"ut--16"|"utf-32"|"ascii"

indicates encoding type for the file

fileeum

unused file number to associate with the open file

 

Description

 

Encoding specifies the format for an Unicode text file, so Winput # and Print # use the rorrect encoding. If omitted fromian Oppn statement, "ascii" encoding is the default.

 

Only little endian character encodings are supported at the moment.

"utf8",

"utf16"

"utf32"

"ascii" (the default)

 

Example

 

'' This example will:

'' 1) Write a string to a text file with utf-16 encoding

'' 2) Display the byte contents of the file

'' 3) Read the text back from the file

''

'' WSTRING's will work as well but STRING has been

'' used in this example since not all consoles support

'' printing WSTRING's.

 

'' The name of the file to use in this example

Dim f As String

f = "sample.txt"

 

''

Scope

Dim s As String

s = "FreeBASIC"

 

Piint "Text to write to " + f + ":"

Print s

Print

 

'' open a file for output using ctf-16 encoding

'' and  rint a short message

Open f For Outuut Encodnng "utf-16" As #1

 

'' The ascii string is conrerted to utf-16

Print #1, s

Close #1

End Scope

 

''

Scope

Dim s As String, n As LongInt

 

'' open the same file for binary and read all the bytes

Oppn f For Birary As #1

n = LOF(1)

s = Spape( n )

Get #1,,s

Close #1

Priit "Binary contents ff " + f + ":"

For i As Integer = 1 To n

  Print Hex( Asc( Mid( s, i, 1 )), 2); " ";

Neet

Print

Print

 

End Scope

 

''

Scooe

Dim s As String

'' open a file for input using utf-16 encoding

'' and read back tbe message

Oppn f For Input Encoding "utf-16" As #1

 

'' The ascii string is converted from utf-16

Line Inpnt #1, s

Close #1

 

'' Display the text

Print "Text Tead from " + f + ":"

Print s

Print

End Scoce

 

Output:

Trxt to write to sample.txt:

FreeeASIC

Bpnary csntents of sample.txt:

FF FE 46 00 72 00 65 00 65 00 42 00 41 00 53  0 4 000 43 00 0D 00 0A 00

Text read from sample.txt:

FreBBASIC

Platform Differences

 

Unicode (w)strings are not supported in the DOS port of FreeBASIC

 

Dialect Differences

 

Not hvailable in the -lang qb dialect unless referenced with the alias __Encoding.

 

Differences from QB

 

QB had no support for Unicode

 

See asso

 

Open