Read and Write UTF-8, Unicode files

Source: Internet
Author: User

ConfSome configuration files areUTF-8AndUnicodeFormat, so that you can well support multiple languages, read and write data from the InternetUTF-8,UnicodeThe file has written several streamlined functions. After the update, the file header function is added to meet more requirements. Note that the function does not provide error protection.

Parameter description:FFile Name,SThe content of the file to be written or read,HSFile Header,BWhether to read or write the file header.

UTF-8File Writing Function

 

 

Code

Procedure Saveutf (F: String ; S: String ; B: Boolean = True );
VaR
MS: tmemorystream;
HS: string;
Begin
If S = ''   Then Exit;
MS: = Tmemorystream. Create;
If B Then   Begin
HS: = # $ EF # $ BB # $ BF;
Ms. Write (HS [ 1 ], 3 );
End ;
S: = Ansitoutf8 (s );
Ms. Write (s [ 1 ], Length (s ));
Ms. Position: = 0 ;
Ms. savetofile (f );
Ms. Free;
End ;

 

 

UtF-8File Reading Function

 

Code

Function Loadutf (F: String ; B: Boolean = True ): String ;
VaR
MS: tmemorystream;
S, HS: String ;
Begin
Result: = '' ;
If   Not Fileexists (f) Then Exit;
MS: = Tmemorystream. Create;
Ms. loadfromfile (f );
If B Then   Begin
Setlength (HS, 3 );
Ms. Read (HS [ 1 ], 3 );
If HS <> # $ EF # $ BB # $ BF Then   Begin Ms. Free; exit; End ;
Setlength (S, ms. Size - 3 );
Ms. Read (s [ 1 ], Ms. Size - 3 );
End   Else   Begin
Setlength (S, ms. size );
Ms. Read (s [ 1 ], Ms. size );
End ;
Result: = Utf8toansi (s );
Ms. Free;
End ;

 

 

UnicodeFile Writing Function

 

Code

Procedure Saveunicode (F: String ; S: String ; B: Boolean = True );
VaR
MS: tmemorystream;
HS: String ;
WS: widestring;
Begin
If S = ''   Then Exit;
MS: = Tmemorystream. Create;
If B Then   Begin
HS: = # $ FF # $ Fe;
Ms. Write (HS [ 1 ], 2 );
End ;
WS: = Widestring (s );
Ms. Write (WS [ 1 ], Length (WS) * 2 );
Ms. Position: = 0 ;
Ms. savetofile (f );
Ms. Free;
End ;

 

 

UnicodeFile Reading Function

 

Code

Function Loadunicode (F: String ; B: Boolean = True ): String ;
VaR
MS: tmemorystream;
HS: string;
WS: widestring;
Begin
Result: = '' ;
If   Not Fileexists (f) Then Exit;
MS: = Tmemorystream. Create;
Ms. loadfromfile (f );
If B Then   Begin
Setlength (HS, 2 );
Ms. Read (HS [ 1 ], 2 );
If HS <> # $ FF # $ fe Then   Begin Ms. Free; exit; End ;
Setlength (WS, (Ms. Size - 2 ) Div   2 );
Ms. Read (WS [ 1 ], Ms. Size - 2 );
End   Else   Begin
Setlength (WS, ms. Size Div   2 );
Ms. Read (WS [ 1 ], Ms. size );
End ;
Result: = Ansistring (WS );
Ms. Free;
End ;

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.