delphi读取文本文件的问题

hn_tzy 2006-07-20 08:51:47
procedure TForm1.Button1Click(Sender: TObject);
var
fName:string;
sqlFile:TextFile;
sqlText:string;
begin
if (self.OpenDialog1.Execute)then
fName := self.OpenDialog1.FileName
else
Exit;
AssignFile(sqlFile,fName);
Reset(sqlFile);
while not Eof(sqlFile) do
begin
Readln(sqlFile,sqlText);
self.Memo1.Lines.Add(sqlText)
end;
CloseFile(sqlFile);
end;

我用上面过程读取文件时,中文是乱码,但是用记事本和C#读出时却正常,请问高手是什么原因,读取的文件是TXT文件
...全文
619 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
李白丢了墨镜 2006-07-24
  • 打赏
  • 举报
回复
路過,雲裏霧裏,學習。
zhou13 2006-07-21
  • 打赏
  • 举报
回复
同上(2)
文件可能是utf存储的。试试:
。。。
wudi_1982 2006-07-21
  • 打赏
  • 举报
回复
字符集问题,你用记事本打开之后,然后点另存,看看是什么字符集,估计是unicode。

实现unicode文件的读取,unicode->Ansi

function GetFileText(const FileName: string): string;
var
hFile: DWORD;
iSize: DWORD;
sTemp: string;
begin
// 打开文件
hFile := CreateFile(PChar(FileName), GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
// 文件大小
iSize := GetFileSize(hFile, nil);
// 申请内存
SetLength(sTemp, iSize + 2);
// 读入文件
ReadFile(hFile, sTemp[1], iSize, iSize, nil);
// 关闭文件
CloseHandle(hFile);
// 置结束符
sTemp[iSize + 1] := #0;
sTemp[iSize + 2] := #0;
// 编码格式
if (sTemp[1] = #$FF) and (sTemp[2] = #$FE) then // UNICODE
begin
// 申请内存
SetLength(Result, iSize);
// 转换编码
WideCharToMultiByte(CP_ACP, 0, PWideChar(@sTemp[3]), -1, @Result[1], iSize, nil, nil);
end else
Result := sTemp;
// 去掉多余字符
Result := string(PChar(Result));
end;

keyz 2006-07-21
  • 打赏
  • 举报
回复
文件可能是utf存储的。试试:

var
fName:string;
sqlFile:TextFile;
sqlText:string;
ws:widestring;
begin
if (self.OpenDialog1.Execute)then
fName := self.OpenDialog1.FileName
else
Exit;
AssignFile(sqlFile,fName);
Reset(sqlFile);
while not Eof(sqlFile) do
begin
Readln(sqlFile,sqlText);
ws:=sqltext;
self.Memo1.Lines.Add(ws);
end;
CloseFile(sqlFile);
end;
andyzhou1101 2006-07-20
  • 打赏
  • 举报
回复
跟memo的字体设置有关

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧