Best way to read text file.

ikaplan

Member
Licensed User
Hello,

I would like to ask a little advice, what would be the best way to read text files into TextBox control?
I just need to read a simple text file and display it's context in TextBox. I thought, the best way would be to use FileReadToEnd() function, however when I read text file using this function I am loosing all line breaks, text is displayed all on one line with no line breaks.
I was trying to read file using FileRead() function in the loop, however while doing that I always receive an exception:
"Input string is not in the correct format"
Not sure, if it is because of file containing some non-ascii characters, however it should not be, the simple text file. I was trying to read using FileRead several text files, they all give me that exception.
Please tell me, what would be the best way to read the text file and output it to the TextBox control with line breaks and with no errors regardless of file context.
Many thanks in advance for any help.

Igor.
 

ikaplan

Member
Licensed User
Hi Erel,

Well, FileReadToEnd does not really changes text from file, however it just gets whole file as one stream regardless of line breaks.
I have attached file igor.txt as an example. This is file created in notepad and in notepad it has 4 lines.

line1
line 2
line 3
line 4

However when I read this file using FileReadToEnd, I get all 4 lines together and TextBox contains just 1 line.
line 1line 2line 3line 4

Thanks a lot.

Igor.
 

Attachments

  • igor.txt
    32 bytes · Views: 217

Mr_Gee

Active Member
Licensed User
Longtime User
Why don't you use the "normal" way?
B4X:
Sub Read
FileOpen(c2,"igor.txt",cRead) 'opens the specified file
s = FileRead(c2) 
Do Until s = EOF 'Read the txt items until end of file.
   s = FileRead(c2)
if NOT(s ="@eof@") Then 'checks if it is the last entry of the file
'add to textbox

End If 
  Loop
  FileClose(c2)
End Sub
the only annoying thing is that it won't read the first line in the above example.
I usally add a comment in the first line
 
Last edited:

ikaplan

Member
Licensed User
Erel,

Thanks a lot, it works fine. Looks like my problem was not in FileReadToEnd function but with TextBox, which was not multiline. :) Many thanks for your help
 

jamesz

New Member
The following code reads your file properly:
'TextBox1 is a multiline textbox.
B4X:
Sub Globals
    'Declare the global variables here.

End Sub

Sub App_Start
    Form1.Show
    FileOpen(c,"igor.txt",cRead)
    TextBox1.Text = FileReadToEnd(c)
    FileClose(c)
End Sub
The result is:
line 1
line 2
line 3
line 4


I fond that has some problom when use above method when the textfile contain chinese .
is it some bug in it??
 

WhiteRussian

New Member
Im a newbie at this, Tho I am fermiliur with .NET from Visual Basic.
The code to read a text file to end:

FileOpen(c1,"igor.txt",cRead) 'opens the specified file
text = FileReadToEnd(c1) 'Read the file into varible "text"
close(c1)
textbox1.text = text 'Assings the varible "text" as the textbox text

If you whant to add text to the textbox use StrAdd, If you whant to replace words use StrReplace or if you whant to remove words use StrRemove.

Im working on a text editor now right now.
 
Last edited:
Top