Delphi - 将文件读取到StringList,然后删除并写回文件

发布于 2024-10-10 22:13:45 字数 1339 浏览 0 评论 0原文

我目前正在开发一个在 Delphi 2010 中生成文件哈希的程序。作为此过程的一部分,我可以选择创建用户预设,例如用户可以创建/保存/删除的哈希算法的预定义选择。我的创建和加载代码工作正常。它使用组合框并从文件“fhpre.ini”加载,该文件内是以以下格式存储的用户预设:-

PresetName
PresetCode(一个 12 位字符串,使用 0 表示不散列,使用 1 表示执行)

在应用程序加载时,它会将此文件中的数据加载到 ComboBox 和一个数组中,其中 ComboBox 的 ItemIndex 与 ComboBox 中相应的正确的 0 和 1 字符串相匹配。大批。

现在我需要实现一项功能,让用户从列表中删除预设。到目前为止,我的代码如下,

procedure TForm1.Panel23Click(Sender : TObject);

var
fil : textfile;
contents : TStringList;
x,i : integer;
filline : ansistring;
filestream : TFileStream;

begin //Start Procedure

//Load data into StringList
contents := TStringList.Create;
fileStream := TFileStream.Create((GetAppData+'\RFA\fhpre.ini'), fmShareDenyNone);
Contents.LoadFromStream(fileStream);
fileStream.Destroy();

//Search for relevant Preset
i := 0;
if ComboBox4.Text <> Contents[i] then
begin
Repeat
i := i + 1;
Until ComboBox4.Text = Contents[i];
end;

contents.Delete(i); //Delete Relevant Preset Name
contents.Delete(i); //Delete Preset Digit String

//Write StringList back to file.
AssignFile(fil,(GetAppData+'\RFA\fhpre.ini'));
ReWrite(fil);
for i := 0 to Contents.Count -1 do
WriteLn(Contents[i]);
CloseFile(fil);
Contents.Free;
end;

但是如果运行此代码,当它到达 WriteLn 部分时,我会收到 105 错误。我知道代码不是很好,例如没有对同名预设的检查,但这会发生,我想首先让基本代码正常工作,然后可以调整并添加额外的检查等。

任何帮助将不胜感激。

I'm currently working on a program to generate the hashes of files, in Delphi 2010. As part of this I have a option to create User Presets, e.g. pre-defined choice of hashing algo's which the user can create/save/delete. I have the create and load code working fine. It uses a ComboBox and loads from a file "fhpre.ini", inside this file is the users presets stored in format of:-

PresetName
PresetCode (a 12 digit string using 0 for don't hash and 1 for do)

On application loading it loads the data from this file into the ComboBox and an Array with the ItemIndex of ComboBox matching the corrisponding correct string of 0's and 1's in the Array.

Now I need to implement a feature to have the user delete a preset from the list. So far my code is as follows,

procedure TForm1.Panel23Click(Sender : TObject);

var
fil : textfile;
contents : TStringList;
x,i : integer;
filline : ansistring;
filestream : TFileStream;

begin //Start Procedure

//Load data into StringList
contents := TStringList.Create;
fileStream := TFileStream.Create((GetAppData+'\RFA\fhpre.ini'), fmShareDenyNone);
Contents.LoadFromStream(fileStream);
fileStream.Destroy();

//Search for relevant Preset
i := 0;
if ComboBox4.Text <> Contents[i] then
begin
Repeat
i := i + 1;
Until ComboBox4.Text = Contents[i];
end;

contents.Delete(i); //Delete Relevant Preset Name
contents.Delete(i); //Delete Preset Digit String

//Write StringList back to file.
AssignFile(fil,(GetAppData+'\RFA\fhpre.ini'));
ReWrite(fil);
for i := 0 to Contents.Count -1 do
WriteLn(Contents[i]);
CloseFile(fil);
Contents.Free;
end;

However if this is run, I get a 105 error when it gets to the WriteLn section. I'm aware that the code isn't great, for example doesn't have checks for presets with same name, but that will come, I want to get the base code working first then can tweak and add extra checks etc.

Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

Smile简单爱 2024-10-17 22:13:45

我希望您知道 TStringList 有 LoadFromFile 和 SaveToFile 方法吗?

如果由于某种原因无法使用这些方法,为什么使用流进行读取而使用 WriteLn 进行写入呢?

要使用 WriteLn 写入文件,您必须将该文件指定为第一个参数:

 WriteLn(fil, Contents[i]);

如果没有该参数,它会尝试写入控制台(这可能在您的 Windows 应用程序中不可用)。错误 105 是“文件未打开以进行输出”。

You are aware, I hope, that TStringList has LoadFromFile and SaveToFile methods?

And if you can't use those methods for some reason, why use a stream for reading but WriteLn for writing?

To write to a file using WriteLn, you must specify the file as the first argument:

 WriteLn(fil, Contents[i]);

without the argument it tries to write to the console (which is presumably not available in your Windows application). Error 105 is "File not open for output".

草莓酥 2024-10-17 22:13:45

由于您正在处理 .ini 文件,因此您应该使用 TIniFile 类根据需要操作其内容。这将使您的配置和代码更易于维护。

Since you are dealing with an .ini file, you should be using the TIniFile class to manipulate its contents as needed. That will make your configuration and code much easier to maintain.

雨后彩虹 2024-10-17 22:13:45

以下是实现 TStringlist.LoadFromFile 和 TStringList.SaveToFile 后的最终代码。它可能仍然可以从一些优化中受益,但这会及时实现。

Procedure TForm1.Panel23Click(Sender : TObject);

var
contents : TStringList;
i : integer;

begin //Start Procedure

//Load data into StringList
Contents := TStringList.Create;
Contents.LoadFromFile((GetAppData+'\RFA\fhpre.ini'));

//Search for relevant Preset
i := 0;
if ComboBox4.Text <> Contents[i] then
begin
   Repeat
    i := i + 1;
   Until ComboBox4.Text = Contents[i];
end;


contents.Delete(i); //Delete Relevant Preset Name
contents.Delete(i); //Delete Preset Digit String
Contents.SaveToFile((GetAppData+'\RFA\fhpre.ini'));

AddPresetCombo(GetAppData+'\RFA\fhpre.ini');   //Populate Comobo With Presets From File
Form1.ComboBox4.ItemIndex := 0;
Contents.Free; 
end;   

Here is what the final code looks like after implementing TStringlist.LoadFromFile and TStringList.SaveToFile. It could probably still benifit from some optimization but that will come in time.

Procedure TForm1.Panel23Click(Sender : TObject);

var
contents : TStringList;
i : integer;

begin //Start Procedure

//Load data into StringList
Contents := TStringList.Create;
Contents.LoadFromFile((GetAppData+'\RFA\fhpre.ini'));

//Search for relevant Preset
i := 0;
if ComboBox4.Text <> Contents[i] then
begin
   Repeat
    i := i + 1;
   Until ComboBox4.Text = Contents[i];
end;


contents.Delete(i); //Delete Relevant Preset Name
contents.Delete(i); //Delete Preset Digit String
Contents.SaveToFile((GetAppData+'\RFA\fhpre.ini'));

AddPresetCombo(GetAppData+'\RFA\fhpre.ini');   //Populate Comobo With Presets From File
Form1.ComboBox4.ItemIndex := 0;
Contents.Free; 
end;   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文