打开ANSI文件并使用Delphi保存Unicode文件 [英] Open an ANSI file and Save a a Unicode file using Delphi
问题描述
以编程方式打开这些文件并将其另存为unicode文件是最简单的方法?我知道我可以通过在记事本中打开每个文件,然后保存为相同的文件,但是在保存为对话框的编码部分中选择了unicode,但是我需要在程序中减少支持调用。
这个问题很容易复制,只需在目录中创建一个* .txt文件,将其重命名为* .UDL,然后使用微软编辑器进行编辑。然后在记事本中打开它,并将其另存为ANSI编码文件。尝试从udl编辑器打开udl,它会告诉你它的损坏。然后将其保存(使用记事本)作为Unicode编码文件,它将会正确打开。
这很简单使用我的 TGpTextFile 单元。我会把一个简短的样本放在这里。
新的Delphi 2009也应该很简单 - 你可能会使用它吗?
编辑:这是他如何使用我的东西在2009年前的Delphis。
var
strAnsi:TGpTextFile;
strUnicode:TGpTextFile;
begin
strAnsi:= TGpTextFile.Create('c:\0\test.udl');
try
strAnsi.Reset; //你也可以在这里指定非默认的8位代码页
strUnicode:= TGpTextFile.Create('c:\0\test-out.udl');
try
strUnicode.Rewrite([cfUnicode]);
而不是strAnsi.Eof do
strUnicode.Writeln(strAnsi.Readln);
finally FreeAndNil(strUnicode);结束;
finally FreeAndNil(strAnsi);结束;
结束
许可证:上面的代码片段属于公共域。无论如何,您可以使用它。
For some reason, lately the *.UDL files on many of my client systems are no longer compatible as they were once saved as ANSI files, which is no longer compatible with the expected UNICODE file format. The end result is an error dialog which states "the file is not a valid compound file".
What is the easiest way to programatically open these files and save as a unicode file? I know I can do this by opening each one in notepad and then saving as the same file but with the "unicode" selected in the encoding section of the save as dialog, but I need to do this in the program to cut down on support calls.
This problem is very easy to duplicate, just create a *.txt file in a directory, rename it to *.UDL, then edit it using the microsoft editor. Then open it in notepad and save as the file as an ANSI encoded file. Try to open the udl from the udl editor and it will tell you its corrupt. then save it (using notepad) as a Unicode encoded file and it will open again properly.
This is very simple to do with my TGpTextFile unit. I'll put together a short sample and post it here.
It should also be very simple with the new Delphi 2009 - are you maybe using it?
EDIT: This his how you can do it using my stuff in pre-2009 Delphis.
var
strAnsi : TGpTextFile;
strUnicode: TGpTextFile;
begin
strAnsi := TGpTextFile.Create('c:\0\test.udl');
try
strAnsi.Reset; // you can also specify non-default 8-bit codepage here
strUnicode := TGpTextFile.Create('c:\0\test-out.udl');
try
strUnicode.Rewrite([cfUnicode]);
while not strAnsi.Eof do
strUnicode.Writeln(strAnsi.Readln);
finally FreeAndNil(strUnicode); end;
finally FreeAndNil(strAnsi); end;
end;
License: The code fragment above belongs to public domain. Use it anyway you like.
这篇关于打开ANSI文件并使用Delphi保存Unicode文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!