Saving Textfile Problems

jpvniekerk

Active Member
Licensed User
Longtime User
I am saving data to a text file, but does not get new lines (as expected) when opening the files with Notepad. It seems like the CRLF is ignored.

I've tried TextWriter with the following code
B4X:
Dim FileName As String
Dim FileDir As String
Dim Txt As String
Dim TW1 As TextWriter
FileName = "TestTextWriter.txt"
FileDir = File.DirRootExternal
TW1.Initialize(File.OpenOutput(FileDir,FileName,False))
For i = 1 To 10
   Txt = "This is line "&i&CRLF
   TW1.WriteLine(Txt)
Next
TW1.Close

... and with WriteList with the following code
B4X:
Dim FileName As String
Dim FileDir As String
Dim Txt As String
Dim Data As List
FileName = "TestWriteList.txt"
FileDir = File.DirRootExternal
For i = 1 To 10
   Txt = "This is line " & i & CRLF
   Data.Add(Txt)
Next
File.WriteList(FileDir,FileName,Data)

The files created with each of the above is attached

Am I missing something?

What do I need to do to get the lines to show as separate lines?
 

Attachments

  • TestTextWriter.txt
    161 bytes · Views: 301
  • TestWriteList.txt
    161 bytes · Views: 309

Theera

Well-Known Member
Licensed User
Longtime User
I am saving data to a text file, but does not get new lines (as expected) when opening the files with Notepad. It seems like the CRLF is ignored.

Hi jpvniekerk,
I think you have same problem. MC73 told that before you save as original file,
must press enter 1 time

aaa
bbb
ccc <= don't save as while in this line,you must press 1 enter key before save as and don't forget save as in UTF-8

Best Regards
Theera
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
Funny, but CRLF is the constant for end of line character in Android. As Android is based on Linux this character is chr(10), not Cr+LF ...
So, add at the end not CRLF, but CHR(13) & CHR(10) and it will work as you expect
Marco
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
As thedesolatesoul wrote, there's no need either for crlf, neither for chr(13)&chr(10) (which I use when I'm appending to a stringBuilder) when you're using writeLine. After all, this is its puprose, to write a line, thus it includes the crlf. At least, this is what I suppose.
 
Upvote 0

rtesluk

Member
Licensed User
Longtime User
Try Chr(13)

B4X:
'Your Code
Txt = "This is line " & i & CRLF
'Try this
Txt = "This is line " & i & Chr(13)

Ray Tesluk :sign0104:
Port Hope Ontario Canada
 
Upvote 0

jpvniekerk

Active Member
Licensed User
Longtime User
Thanks all for your help... I've learned yet again through this forum!

@mc73 - this is what I also supposed - hence my surprise.

So this is how it works (as I understand):
WriteList (and WriteLine) adds a Chr(10) at the end of each line. This does not show as a new line in Notepad, but it does in Notepad++ (?!?!).
To make a text file appear "as expected" in Notepad you have to add a Chr(13) to the end of each line. This does not seem to have any effect (make it look different) in Notepad++.

Now I know!
 
Last edited:
Upvote 0

Roger Garstang

Well-Known Member
Licensed User
Longtime User
Just was typing the same thing and ya beat me to it...

Notepad++ detects file types and knows how to handle Linux files. CR and LF are a carry over from line printers. Think of LF as Y+1 and CR as X=0. If you wanted to print text bold you'd just issue a CR and reprint over your text, etc. DOS/Win kept them both so printer files could easily be transferred. Linux just considered LF to be the same as Newline and makes the files smaller. I could go either way on which is better much like IDEs that are switching to what some call Tab Compression where spaces are replaced with tabs for easier file processing and smaller file sizes since code is usually indented.
 
Last edited:
Upvote 0
Top