Page 1 of 1

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

Posted: Sun Apr 17, 2022 4:27 pm
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

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

Posted: Sun Apr 17, 2022 4:39 pm
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)

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

Posted: Sun Apr 17, 2022 4:39 pm
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)

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

Posted: Sun Apr 17, 2022 4:54 pm
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:

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

Posted: Sun Apr 17, 2022 5:14 pm
by davebar
Thanks mk-soft & STARGATE for the important pointers
and Marc56us for the detailed explanation.
Regards Dave