output textfile

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
Hello,
using this code, the output file "Risultato.txt" is empty. Why?

'Read text File
ret = fd.Show("Choose a file to load:", "Okay", "Cancel", "", Null)
If ret = -3 OR fd.ChosenName = "" Then
Return
End If
If File.Exists(fd.FilePath, fd.ChosenName) = False Then
Msgbox(fd.ChosenName & " does not exist.", "")
Return
End If

Dim TR As TextReader
Dim TW As TextWriter
Dim s As String

TR.Initialize(File.OpenInput(fd.FilePath, fd.ChosenName))
TW.Initialize(File.OpenOutput(File.DirRootExternal & "/temp/output", "Risultato.txt", False ))

Do While s <> Null
s = TR.ReadLine
If s = Null Then
Else
s = xn_crypt.xn_trim_encrypt(s)
lbFile.Text = lbFile.Text & " " & s
TW.WriteLine(s)
Log(s)
End If
Loop
 

mc73

Well-Known Member
Licensed User
Longtime User
Things missing in your code. For e.g. what is following the 'then'? An 'else'? Not good, or perhaps bad copy-paste. Another instance of s.tr.readline should be put once outside of the do...loop. In general, it's better to use the code tags while posting.
 
Upvote 0

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
Things missing in your code. For e.g. what is following the 'then'? An 'else'? Not good, or perhaps bad copy-paste. Another instance of s.tr.readline should be put once outside of the do...loop. In general, it's better to use the code tags while posting.

I don't need correction for the code but I wish to know why the file is empty
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
I don't need correction for the code but I wish to know why the file is empty

When you start the Do While loop, s is Null as you have just created it. As far as I am aware this will result in the loop terminating without running (the while is evaluated at the start not the end). So possibly a bit of code correction would not go amiss
B4X:
s = TR.ReadLine
Do While s <> Null
s = xn_crypt.xn_trim_encrypt(s)
lbFile.Text = lbFile.Text & " " & s
TW.WriteLine(s)
Log (s)
s = TR.ReadLine
Loop
 
Last edited:
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
Or:
B4X:
Dim updated As List
updated.Initialize
For Each line As String In File.ReadList(...)
 updated.Add(xn_crypt.xn_trim_encrypt(line)
Next
File.WriteList(updated, ...)

I did think that it cried out for a For Each, but wanted to keep it to what the OP was comfortable with.
 
Upvote 0
Top