读写UTF-8、Unicode文件(加上了文件头,貌似挺好用)

conf配置文件一些为UTF-8和Unicode格式,这样便可良好的支持多语言,从网上查阅资料后,将读写UTF-8、Unicode文件写了几个最精简的函数,更新后加了是否写文件头的功能,以适应更多需要,注意函数未加防错保护。

参数说明:f文件名、s写入或读取的文件内容、hs文件头、b是否读写文件头。

UTF-8文件写入函数

代码

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-8文件读取函数

代码

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;

Unicode文件写入函数

代码

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;

Unicode文件读取函数

代码

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;

http://www.cnblogs.com/kfarvid/archive/2010/11/10/1873403.html

时间: 2024-08-03 11:24:13

读写UTF-8、Unicode文件(加上了文件头,貌似挺好用)的相关文章

在py文件中设置文件头

在写python文件的时候有时需要记录作者.创建时间等时间,因此可以给python文件设置文件头,这里以PyCharm为例介绍设置步骤: 1. 打开PyCharm,依次点击Setting-----Editor------File and Code Template------Python Script,出现如图所示对话框 2. 在图示的方框里输入以下代码,点击OK #!/usr/bin/python3 # --*-- coding: utf-8 --*-- # @Author: your nam

各种常见文件的hex文件头

我们在做ctf时,经常需要辨认各种文件头,跟大家分享一下一些常见的文件头. 扩展名 文件头标识(HEX) 文件描述 123 00 00 1A 00 05 10 04 Lotus 1-2-3 spreadsheet (v9) file 3gg; 3gp; 3g2 00 00 00 nn 66 74 79 70 33 67 70 3rd Generation Partnership Project 3GPP (nn=0x14)   and 3GPP2 (nn=0x20) multimedia fil

Dev c++在新建文件中插入文件头

在菜单栏中依次点击Tools->Editor Options->Snippets->Default Source 勾选Insert the following code into every new empty file 输入自己模板点击OK保存.Date后面可以加<DATETIME>显示日期 /* Name: Copyright: Author: Date: <DATETIME> Description: */ #include<iostream>

非Unicode工程读取Unicode文件

MyUnicodeReader.h #pragma once /************************************************************************/ /* 在“多字节字符集”属性的工程中读取Unicode文件 ** -----------------------------------注意------------------------------------------------------- ** -------------一定

利用文件头判断文件类型

上传文件时经常需要做文件类型判断,例如图片.文档等,普通做法是直接判断文件后缀名,而文艺青年为了防止各种攻击同时也会加上使用文件头信息判断文件类型. 原理很简单:用文件头判断,直接读取文件的前2个字节即可. 1 public class FileUtil { 2 /** 3 * byte数组转换成16进制字符串 4 * 5 * @param src 6 * @return 7 */ 8 private static String bytesToHexString(byte[] src) { 9

java通过文件头来判断文件类型

import java.io.FileInputStream; import java.io.IOException; import java.util.HashMap; import java.util.Map.Entry; /** * @Description 根据的文件头来判断文件类型 * @author LJ * @Version v1.0 */ public class GetFileTypeByHead { public static void main(String[] args)

[C/C++标准库]_[读写中文路径的文件--写入unicode字符串]

场景: 1. 需要写入非ascii文本并且与本地编码无关时,除了utf8,unicode编码是另外一个选择,它的好处是占两个字节,便于统计字符和对字符进行处理,因为有对应的宽字节的函数,如wcslen. 2.需要注意的亮点,要先写入0xff,0xfe文件头,之后使用fwprintf时用%S(大写)格式写入宽字节字符串. 3.使用_wfopen支持中文路径. 代码1: #include <stdio.h> #include <stdint.h> #include <stdlib

【代码备忘】C++ fstream 读写 unicode 文件

欢迎加入我们的QQ群,无论你是否工作,学生,只要有c / vc / c++ 编程经验,就来吧!158427611 所谓的unicode文件,无非就是在文件头部插入了 0xFFFE的标志...读写的时候对应的读写 就可以了. namespace fileStream { bool readFile_Unicode( const string &file ,wstring &destWstring) { destWstring.clear(); //setlocale(LC_ALL,"

fstream读写UNICODE文件

今天遇到要处理UNICODE文件的情况,网上找了一圈都是读取出字节,再转的,这个不方便啊!想起了有codecvt这么个东西,顺藤摸瓜,找到了方法. locale utf16(locale(""), new codecvt_utf16<wchar_t, 0x10ffff, little_endian>); wifstream fin("Module.rc"); wofstream fout("Module.rc.tt", ios_bas