reg文件读取,解决问题送200分。 急!!!!(0分)

  • 主题发起人 主题发起人 lb3141
  • 开始时间 开始时间
L

lb3141

Unregistered / Unconfirmed
GUEST, unregistred user!
这个问题我提过2次,没人回答出正确答案,现在从提,解决问题的送200分,不够在加!

读取.reg文件的问题,如果reg文件中有中文,就出现乱码,如果全是字母,则正确,不知道是怎么回事?(*文件一定要从注册表导出,如果手工建,这虽然内容一样,但其内在格式不一样。导出的文件若没有汉字,你可以随意加几个汉字,那读出的结果就不同了,有乱码。手工创建的reg文件就是好的)

导出的.reg文件如下:(我把导出文件手动添加了汉字)

++++++++++++++++++++++++++++++++++++++++++++++

Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE/SOFTWARE/ODBC/AAA]
"Translator"="C://WINDOWS//system32//AAA32.dll"
"Setup"="C://WINDOWS//system32//BBB32.dll"
"UsageCount"="这是汉字,读出后有乱码"
"KEY"="ABC"

++++++++++++++++++++++++++++++++++++++++++++++

以下是用Delphi实现的读取代码:
(用FileStream,File of Byte,stringlist我都试过,都不行)

var
s1,s2: textFile;
i: integer;
ch: char;
chs: array [0..2048] of char;
begin
AssignFile(s1,'aaa.reg'); Reset(s1);
//跳过文件格式 FFFE
read(s1,ch); read(s1,ch);
i :=0;
FillChar(chs,sizeof(chs),#0);
while not eof(s1) do
begin
read(s1,ch);
if ch <> #0 then
begin
chs :=ch;
inc(i);
end;
if ch = #13 then
begin
i :=0;
Memo1.Lines.Add(Trim(chs));
FillChar(chs,sizeof(chs),#0);
end;
end;
CloseFile(s1);

读出有汉字的地方出现乱码,其他地方都正常。
我想原因在于导出的注册表文件开头有FFFE标志,被认为是UTF-8编码,所以形成了乱码。
所以请教各位大虾不吝赐教!如何能读出正确的内容???
 
使用widechar...或者pchar...
 
没有加判断中英文字符,
 
关键是怎么实现呀??
 
是字符问题引发的,
请使用UNICODE字符处理,
具体使用WIDECHAR代替CHAR的声明
 
请使用UNICODE字符处理,
 
谁能给出源码呢?试了,都不行呀?
 
var
readinfo:Tregistry;
UsageCount:string;

readinfo:=Tregistry.create;
readinfo.root:=HKEY_LOCAL_MACHINE;
if readinfo.openkey("SOFTWARE/abs/abc",false) then
begin
UsageCount:=readinfo.readstring("这是汉字,读出后有乱码");
showmessage(UsageCount);
end;

这样,showmessage并没有出现乱码啊!
 
我要的是对文件操作,注册表操作我还不会呀??
 
请使用下面方法试试
function WordToWStr(Src: array of Byte; ALen: Integer): WideString;
var
SrcWord: Word;
i, j : Integer;
begin
Result := '';
for i := 0 to ALen div 2 - 2 do
begin
SrcWord := Src[i * 2 + 1];
SrcWord := SrcWord shl 8;
SrcWord := SrcWord or Src[i * 2];
Result := Result + WideChar(SrcWord);
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
s1,s2: textFile;
i: integer;
ch: char;
chs: array [0..2048] of Byte;
begin
AssignFile(s1,'c:/lee.reg'); Reset(s1);
read(s1,ch); read(s1,ch);
i := 0;
FillChar(chs,sizeof(chs),#0);
while not eof(s1) do
begin
read(s1,ch);
chs := ord(ch);
inc(i);
if ch = #10 then
begin
Memo1.Lines.Add(WordToWStr(chs, i));
i := 0;
read(s1,ch); //读掉最后一个宽字符的高位部分(一个BYTE)
FillChar(chs,sizeof(chs),#0);
end;
end;
CloseFile(s1);
end;
 
今验证,jfreechart 你是对的,不过虽然问题我自己已经解决了,但分还是要给你,谢谢!(请你到3525300帖子中领分)
这是我的实现:
type
TTextFormat=(tfAnsi,tfUnicode,tfUnicodeBigEndian,tfUtf8);

function ReadTextFile(const FileName: string; sText: TStrings): TTextFormat;

procedure WordLoHiExchange(var W: Word);
var
B: Byte;
begin
B :=WordRec(W).Lo;
WordRec(W).Lo:=WordRec(w).Hi;
WordRec(W).Hi:=b;
end;

const
TextFormatFlag: array [tfAnsi..tfUtf8] of Word =
(
$0000, //ANSI:无格式定义
$FFFE, //Unicode:前两个字节为FFFE;
$FEFF, //Unicode big endian:前两字节为FEFF
$EFBB //UTF-8:前两字节为EFBB;
);

var
W: Word;
B: Byte;
ContentSize: Integer;
FileContent: WideString;
begin
with TFileStream.Create(FileName,fmOpenRead or fmShareDenyNone) do
try
Read(W,2);
//因为是以Word数据类型读取,故高低字节互换
WordLoHiExchange(W);

ContentSize := (Size-2);

if not Assigned(sText) then
sText :=TStringList.Create;

if W = TextFormatFlag[tfUnicode] then
begin
Result := tfUnicode;
SetLength(FileContent,ContentSize div 2);
ReadBuffer(FileContent[1],ContentSize);
sText.Text := FileContent;
end
else if W = TextFormatFlag[tfUnicodeBigEndian] then
begin
Result := tfUnicodeBigEndian;
//还在想怎么实现????
end
else if W = TextFormatFlag[tfUtf8] then
begin
//这里要注意一下,UFT-8必须要跳过三个字节。
Read(B,1);
Result := tfUtf8;
//还在想怎么实现????
end else
begin
Result := tfANSI;
sText.LoadFromFile(FileName);
//Position :=0;
end;
finally
Free;
end;
end;


上面解决了Ansi个Unicod两种格式,但另外两种格式还在想,不知道能否赐教!!分可另外算
 
我的方法可行吗?
如果可行的话.200分.我如何收到?
 
后退
顶部