Delphi - reading a file in a StringList, then deleting and writing to a file

I am currently working on a program for creating file hashes in Delphi 2010. As part of this, I have the ability to create custom presets, for example. predefined selection of a hashing algorithm that the user can create / save / delete. I have a create and load code that works fine. It uses a ComboBox and is loaded from the file "fhpre.ini", inside this file user presets are stored in the format: -

Presetname
PresetCode (12-bit string using 0 for non-hash and 1 for do)

When loading the application, it loads the data from this file into ComboBox and Array with ItemIndex from ComboBox corresponding to the correct line 0 and 1 in the array.

Now I need to implement a function so that the user removes the 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 done, I get an error 105 when it gets to the WriteLn section. I know that the code is small, for example, it does not have checks for presets with the same name, but this will happen, I want to run the base code first, and then configure and add additional checks, etc.

Any help would be greatly appreciated.

+4
source share
3 answers

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

And if you cannot use these methods for any reason, why use a stream to read, but WriteLn to write?

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

  WriteLn(fil, Contents[i]); 

without the argument that he is trying to write to the console (which is apparently not available in your Windows application). Error 105 - "The file is not open for output."

+12
source

Since you are working with an .ini file, you must use the TIniFile class to manage its contents as needed. This will simplify your configuration and code.

+6
source

Here's what the last code looks like after implementing TStringlist.LoadFromFile and TStringList.SaveToFile. It could probably come in handy from some optimization, but it will happen on 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; 
0
source

Source: https://habr.com/ru/post/1334793/


All Articles