Best way to read text file.

Discussion in 'Questions (Windows Mobile)' started by ikaplan, Oct 8, 2008.

  1. ikaplan

    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.
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    FileReadToEnd should not change the content.
    Can you upload your text file?
     
  3. ikaplan

    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.
     

    Attached Files:

    • igor.txt
      File size:
      32 bytes
      Views:
      67
  4. Mr_Gee

    Mr_Gee Active Member Licensed User

    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: Oct 8, 2008
  5. Erel

    Erel Administrator Staff Member Licensed User

    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
     
  6. ikaplan

    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
     
  7. jamesz

    jamesz New Member


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

    Erel Administrator Staff Member Licensed User

    You should save your file encoded with UTF8. Open it in Notepad and choose Save As, Encoded - UTF8.
     
  9. WhiteRussian

    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: Nov 4, 2008
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice