Recent

Author Topic: Double CR-LF in text files  (Read 3227 times)

Ednay

  • Newbie
  • Posts: 1
Double CR-LF in text files
« on: April 05, 2024, 02:54:45 am »
I am new to this forum. Originally I used Borland Pascal when programming (in Windows XP) which no longer works in Windows 10.

I am still getting used to Free Pascal.

This is a sample file:

This is a file

that has double

CR-LF after every line

How can the second CR-LF

be removed from the original file?
« Last Edit: April 05, 2024, 02:58:30 am by Ednay »

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: Double CR-LF in text files
« Reply #1 on: April 05, 2024, 03:25:02 am »
How can the second CR-LF be removed from the original file?[/i]
Something like this perhaps ?

Code: Pascal  [Select][+][-]
  1. program fix;
  2.  
  3. uses
  4.   classes;
  5.  
  6. var
  7.   sl: TStringList;
  8.   index: integer;
  9. begin
  10.   sl := TStringList.Create;
  11.   sl.LoadFromFile('Show File.txt');
  12.  
  13.   index := 0;
  14.   while index < sl.Count do
  15.   begin
  16.     if sl[index] = ''
  17.      then sl.Delete(index)
  18.       else inc(index);
  19.   end;
  20.  
  21.   sl.SaveToFile('fixed.txt');
  22.   sl.Free;
  23. end.
  24.  
Today is tomorrow's yesterday.

Bart

  • Hero Member
  • *****
  • Posts: 5715
    • Bart en Mariska's Webstek
Re: Double CR-LF in text files
« Reply #2 on: April 05, 2024, 03:49:43 pm »
Alternatively:
Code: Pascal  [Select][+][-]
  1. program fix;
  2.  
  3. uses
  4.   classes, SysUtils;
  5.  
  6. var
  7.   sl: TStringList;
  8.   S: String;
  9. begin
  10.   sl := TStringList.Create;
  11.   sl.LoadFromFile('Show File.txt');
  12.   S := sl.Text;
  13.   S := StringReplace(S,LineEnding+LineEnding,LineEnding,[rfReplaceAll]);
  14.   sl.Text := S; //<<== did forget to add this line, as AlexTP pointed out.
  15.   sl.SaveToFile('fixed.txt');
  16.   sl.Free;
  17. end.

Bart
« Last Edit: April 07, 2024, 10:26:43 pm by Bart »

AlexTP

  • Hero Member
  • *****
  • Posts: 2708
    • UVviewsoft
Re: Double CR-LF in text files
« Reply #3 on: April 06, 2024, 03:45:49 pm »
@Bart,
you missed the putting S to the list back:
  sl.Text := S;

Bart

  • Hero Member
  • *****
  • Posts: 5715
    • Bart en Mariska's Webstek
Re: Double CR-LF in text files
« Reply #4 on: April 06, 2024, 10:48:19 pm »
@Bart,
you missed the putting S to the list back:
  sl.Text := S;
Well, spotted: fixed it by editing my post and adding a comment.

Bart

dseligo

  • Hero Member
  • *****
  • Posts: 1683
Re: Double CR-LF in text files
« Reply #5 on: April 06, 2024, 11:46:27 pm »
@Bart,
you missed the putting S to the list back:
  sl.Text := S;
Well, spotted: fixed it by editing my post and adding a comment.

Bart

Edit again :)
Change s.Text := S; to:
Code: Pascal  [Select][+][-]
  1. sl.Text := S;

Bart

  • Hero Member
  • *****
  • Posts: 5715
    • Bart en Mariska's Webstek
Re: Double CR-LF in text files
« Reply #6 on: April 07, 2024, 10:27:35 pm »
Edit again :)
Change s.Text := S; to:
Code: Pascal  [Select][+][-]
  1. sl.Text := S;

 :-[ :-[ :-[ :-[ :-[ :-[ :-[ :-[ :-[ :-[ :-[ :-[ :-[ :-[ :-[ :-[ :-[ :-[ :-[

Bart

 

TinyPortal © 2005-2018