Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4ppc (Windows Mobile) > Questions (Windows Mobile)
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Questions (Windows Mobile) Post any question regarding Basic4ppc.

Best way to read text file.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-08-2008, 01:41 AM
Newbie
 
Join Date: Dec 2007
Posts: 9
Default Best way to read text file.

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.
Reply With Quote
  #2 (permalink)  
Old 10-08-2008, 04:40 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,726
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

FileReadToEnd should not change the content.
Can you upload your text file?
Reply With Quote
  #3 (permalink)  
Old 10-08-2008, 09:47 AM
Newbie
 
Join Date: Dec 2007
Posts: 9
Default

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.
Attached Files
File Type: txt igor.txt (32 Bytes, 17 views)
Reply With Quote
  #4 (permalink)  
Old 10-08-2008, 09:55 AM
Basic4ppc Veteran
 
Join Date: Feb 2008
Location: Hilversum, The Netherlands
Posts: 295
Awards Showcase
Beta Tester 
Total Awards: 1
Default

Why don't you use the "normal" way?
Code:
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 by Mr_Gee : 10-08-2008 at 10:21 AM.
Reply With Quote
  #5 (permalink)  
Old 10-08-2008, 10:33 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,726
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

The following code reads your file properly:
'TextBox1 is a multiline textbox.
Code:
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
Reply With Quote
  #6 (permalink)  
Old 10-08-2008, 11:17 AM
Newbie
 
Join Date: Dec 2007
Posts: 9
Default

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
Reply With Quote
  #7 (permalink)  
Old 10-26-2008, 05:23 AM
Newbie
 
Join Date: Oct 2008
Posts: 4
Default

Quote:
Originally Posted by Erel View Post
The following code reads your file properly:
'TextBox1 is a multiline textbox.
Code:
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??
Reply With Quote
  #8 (permalink)  
Old 10-26-2008, 05:28 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,726
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

You should save your file encoded with UTF8. Open it in Notepad and choose Save As, Encoded - UTF8.
Reply With Quote
  #9 (permalink)  
Old 11-04-2008, 03:36 PM
Newbie
 
Join Date: Oct 2008
Posts: 3
Default

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 by WhiteRussian : 11-04-2008 at 03:39 PM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
read Exel file? Byak@ Questions (Windows Mobile) 2 10-21-2008 10:30 AM
XML file read stratus Questions (Windows Mobile) 8 06-20-2008 03:03 PM
Upload text file aerohost Questions (Windows Mobile) 2 06-17-2008 05:37 PM
Struggle to read a .txt file into TextBox? HARRY Questions (Windows Mobile) 2 02-26-2008 04:39 PM
CSV file read still slow in V6.01 but other tasks much improved HarleyM Questions (Windows Mobile) 3 01-27-2008 10:18 AM


All times are GMT. The time now is 02:38 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0