Hello. We asked to write a program that allows you to copy even / odd / all lines of the source text file into the resulting file. That is, the program reads a text file, displays it in the Memo-field. Then on the GroupBox form, we have two CheckBox checkboxes, one for even lines, the other for odd lines, and a save button. There is no problem with the implementation of reading a text file. But then there are problems with the allocation of even / odd lines from the mem-field. This is what is given to us for solving this problem, but from this a clear picture does not emerge.

(button) Button OnClick Event: button click

(memo field)
Lines [i] - Appeal to the i-th line of the memo-field. The first line has index 0. `` Clear-clear memo area.
Lines.Add
Lines.LoadFromFile
Lines.Count Number of lines in the memo field.

(component - checkbox) CheckBox Checked Checkbox status: checked (True) / cleared (False). OnClick Event: click on the checkbox

Please tell me how you can implement this program.

  • Minimum code hint: chetnoe: = false; for {* iterate Memo lines } do begin if chetnoe then { write to file *} chetnoe: = not chetnoe; end; If you understand the task, this is enough for you) - Sh4dow

5 answers 5

I didn’t quite understand the task ... You first have to load the entire contents of the text file into Memo, then click the button and, depending on what flags are specified (even or odd lines), select certain lines - and where to write them - load ??? Can I write actions on points?

I would write the DelphiM0ZG variant in another way: there are 3 write variants: even / odd / all

procedure TForm1.Button1Click(Sender: TObject); var start,N:integer; endstrig:TStringList; begin { CheckBox1.Checked ==true => нужны четн CheckBox2.Checked ==true => нужны нечетн } //если нужны все if (CheckBox1.Checked)and(CheckBox2.Checked) then begin //пользуем встроеный метод Memo.Lines.SaveToFile('путь к файлу'); //выходим с процедуры exit; end else begin //либо чет либо нечет if (CheckBox1.Checked) then begin //четн start:=0; // опред старт поз //опред старт поз If (Odd(Memo.Lines.Count-1)) then N:=Memo.Lines.Count-2 else N:=Memo.Lines.Count-1; end; if (CheckBox2.Checked) then begin //нужны нечетн start:=1; // опред старт поз //опред старт поз If (Odd(Memo.Lines.Count-1)) then N:=Memo.Lines.Count-1 else N:=Memo.Lines.Count-2; end; endstring:=TstringList.Create; //строим цикл while start<=N do begin endstring.Add(Memo.Lines.Strings[start]); start:=start+2;//увелич на 2 ед end; endstring.SaveToFile('путь к файлу'); //удал объект endstring.free; end; end; 

The difference is that we first determine which lines we work with - and then we build the cycle we need in which we do the necessary action, which in my opinion is faster than checking the condition every time in the cycle

  • And if more checkboxes are added? ) - Nofate
  • if the items are painted on items, then: 1. Load a text file into a memo 2. Use CheckBox to select "even" or "odd" lines (for all lines, click both CheckBoxes) 3. Write the selected lines to the new file. In general, the whole task ... - tkoff
  • that is, a unas can have 3 options for writing to a new file: 1. only even 2. only odd 3 all records is false? - Ale_x
 var start:integer; begin if (CheckBox1.Checked) then start:=0 else start:=1; while start<Memo1.Lines.Count do begin Memo2.Lines.Add(Memo1.Lines.Strings[start]); inc(start,2); end end; 

So faster, no?

  • This code is probably the shortest and fastest. I wrote my code hastily, so I did not set up optimization tasks for myself, I wrote it as an example. - DelphiM0ZG
  • And if CheckBox1.Checked == true and CheckBox2.Checked == true, that is, you need to save all the lines to a file, then your code will not work as you need. - Ale_x
  • you enter the step parameter, you initialize before the loop in 1 or 2 and replace inc (start, 2) with inc (start, step) - den94

hmm ...

 for i:=1 to memo.lines.count do begin //четные if (checkbox1.checked) and(i mod 2=0) then записать в файл Memo.lines[i-1]; //не четные if (checkbox2.checked) and(i mod 2=1) then записать в файл Memo.lines[i-1]; end; 

    I think this code will help. In this example, I showed how it is possible to write the necessary strings (even or odd) from one Memo-field to another. There are comments in the code, so everything should be clear.

      procedure TForm3.Button1Click (Sender: TObject);
     Var
       i, N: LongWord;  // data type can be used and Integer
     begin
       Memo2.Clear;  // Pre-clear Memo
       {If sampling of even rows is required, then rows should be chosen.
       with an odd number, since in Memo they are numbered from zero,
       and from people to number something from scratch nobody will guess}
       N: = Memo1.Lines.Count-1;  // number of lines
       For i: = 0 To N Do
         If (CheckBox1.Checked) Then // If the even line,
           If (Odd (i)) Then // Check the line number for oddness
             Memo2.Lines.Add (Memo1.Lines.Strings [i])
           Else // for nested If
         Else
           If Not (Odd (i)) Then // Check the line number for parity (0 is an even number!)
             Memo2.Lines.Add (Memo1.Lines.Strings [i]);
     end;
    

    • comment to your code in my answer - Ale_x

    The numbering of lines in Tmemo starts from 0, the numbering of lines of text - from 1. Therefore: Odd lines - start: = 0, Even lines - start: = 1;