Find [CR][LF] in text file. {SOLVED}

Just starting out? Need help? Post your questions and find answers here.
davebar
User
User
Posts: 82
Joined: Fri Aug 31, 2018 9:23 am
Location: Australia

Find [CR][LF] in text file. {SOLVED}

Post by davebar »

I have been trying all day to work out a very simple string manipulation, but I keep coming up blank. I have a simple text file where sections of the file are identified and separated by a double [CR][LF]. When I ReadFile() and perform a ReadString() I want to locate these double [CR][LF] and strip them from the end of the incoming string.
Here's how the file opens in Notepad++:
https://www.mediafire.com/view/bjk0a3qa ... P.png/file

Code: Select all

CRLF.s = Chr($D) + Chr($A) + Chr($D) + Chr($A)
InFile.s = "Dummy.txt"
Test.s = ""
If ReadFile(0, InFile)
  While Eof(0) = 0
    Test = ReadString(0)
    If FindString(Test, CRLF)
      Debug "FOUND IT!!!"
    Else
      Debug "WTF???"
    Endif
  Wend
CloseFile(0)
Endif
Obviously I am doing something stupid, so any guidance would be welcome.

Dave
Last edited by davebar on Sun Apr 17, 2022 5:13 pm, edited 1 time in total.
User avatar
mk-soft
Always Here
Always Here
Posts: 6202
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Find [CR][LF] in text file.

Post by mk-soft »

A double CRLF marks a blank line.

ReadString Reads up to the line break and does not return the line break (CRLF, LFCR, LF, CR).
Thus, with a double CRLF, the next line is an empty string.

To search yourself, you must load the entire file in a string.

Test = ReadString(0, #PB_File_IgnoreEOL)
Last edited by mk-soft on Sun Apr 17, 2022 4:41 pm, edited 1 time in total.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
STARGÅTE
Addict
Addict
Posts: 2226
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Find [CR][LF] in text file.

Post by STARGÅTE »

ReadString() automatically reads until an end of a line, so all "Test" string will never have any end of line.
You can use: ReadString(0, #PB_File_IgnoreEOL) to read the whole file and use FindString.
Further, #CRLF$ can be used instead of Chr($D) + Chr($A)
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: Find [CR][LF] in text file.

Post by Marc56us »

As mk-soft said: double CRLF = empty line
So, simple count empty string in line

Code: Select all

ReadFile(0, "DoubleCRLF.txt")
While Not Eof(0)
  i + 1
  If ReadString(0) = ""
    j + 1
  EndIf
Wend  
CloseFile(0)
Debug "" + i + " lines"
Debug "" + j + " Empty lines"
And to remove it, write non-empty lines only.

Code: Select all

ReadFile(0, "DoubleCRLF.txt")
OpenFile(1, "DoubleCRLF_New.txt")
While Not Eof(0)
  i + 1
  Tmp_String$ = ReadString(0)
  If Tmp_String$ = ""
    j + 1
  Else
    WriteStringN(1, Tmp_String$)
  EndIf
Wend  
CloseFile(0)
CloseFile(1)
Debug "" + i + " lines"
Debug "" + j + " Empty lines"
"Think simple, think (Pure)basic"
:wink:
davebar
User
User
Posts: 82
Joined: Fri Aug 31, 2018 9:23 am
Location: Australia

Re: Find [CR][LF] in text file. {SOLVED}

Post by davebar »

Thanks mk-soft & STARGATE for the important pointers
and Marc56us for the detailed explanation.
Regards Dave
Post Reply