Results 1 to 15 of 15

Thread: Problems reading text files RESOLVED

  1. #1

    Thread Starter
    Registered User
    Join Date
    Jan 2003
    Posts
    218

    Problems reading text files RESOLVED

    I have these very ordinary text files with lines terminated with char. return and line feeds. When I try to open them using the Open statement and then load into a variable the first line and look for the vbCrLf bit I can't find it.

    Now, if I copy and paste the content of the files and create a new file with a similar name and extension, it works fine.

    Any one know what is wrong here?

    Thanks.
    Last edited by Davos; Sep 18th, 2003 at 09:50 AM.

  2. #2
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    look for Chr(10) and Chr(13)
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  3. #3

    Thread Starter
    Registered User
    Join Date
    Jan 2003
    Posts
    218
    Still same problem.

    any other idea?

    Seems to be something to do with how VB opens the file. Beast me as to what it is.

  4. #4
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,526
    Post the code you are using to read the files.

  5. #5

    Thread Starter
    Registered User
    Join Date
    Jan 2003
    Posts
    218
    VB Code:
    1. FileN = FreeFile
    2.     Open CommonDialog1.FileName For Input Shared As #FileN
    3.         wholeFile = Input(LOF(1), #FileN)      'read whole file
    4.     Close #FileN
    5.     whleF() = Split(wholeFile, vbCrLf, -1, vbTextCompare)   ' create array

  6. #6

  7. #7
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,526
    Try this:

    VB Code:
    1. FileN = FreeFile
    2.     Open CommonDialog1.FileName For Input Shared As #FileN
    3.         wholeFile = Input(LOF(1), #FileN)      'read whole file
    4.     Close #FileN
    5.     If InStr(1,wholeFile,Chr$(13) + Chr$(10)) = 0 Then MsgBox "CrLf not found",,"Error"
    6.     whleF() = Split(wholeFile, vbCrLf, -1, vbTextCompare)   ' create array

  8. #8

    Thread Starter
    Registered User
    Join Date
    Jan 2003
    Posts
    218
    Jd, the point is the crlf is THERE. It's the programme that is not pick it up.

    And using #FileN inside the LOF does not work.

  9. #9
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    From Help

    Editing Files Opened for Sequential Access
    If you want to edit a file, first read its contents to program variables, then change the variables, and finally, write the variables back to the file. The following sections discuss how to edit records opened for sequential access.

    Reading Strings from Files
    To retrieve the contents of a text file, open the file for sequential Input. Then use the Line Input #, Input( ), or Input # statement to copy the file into program variables.

    Visual Basic provides statements and functions that will read and write sequential files one character at a time or one line at a time.

    For example, the following code fragment reads a file line by line:

    Dim LinesFromFile, NextLine As String

    Do Until EOF(FileNum)
    Line Input #FileNum, NextLine
    LinesFromFile = LinesFromFile + NextLine + Chr(13) + Chr(10)
    Loop

    Although Line Input # recognizes the end of a line when it comes to the carriage return–linefeed sequence, it does not include the carriage return–linefeed when it reads the line into the variable. If you want to retain the carriage return–linefeed, your code must add it.

    You can also use the Input # statement, which reads a list of numbers and/or string expressions written to the file. For example, to read in a line from a mailing list file, you might use the following statement:

    Input #FileNum, name, street, city, state, zip

    You can use the Input function to copy any number of characters from a file to a variable, provided the variable is large enough. For example, the following code uses Input to copy the specified number of characters to a variable:

    LinesFromFile = Input(n, FileNum)

    To copy an entire file to a variable, use the InputB function to copy bytes from a file to a variable. Since the InputB function returns an ANSI string, you must use the StrConv function to convert the ANSI string to a UNICODE string as follows:

    LinesFromFile = StrConv(InputB(LOF(FileNum), FileNum), vbUnicode)

  10. #10

    Thread Starter
    Registered User
    Join Date
    Jan 2003
    Posts
    218
    Thanks Martin. It's the hash sign that made it show errors. The code still does not work. I'm posting the text file I work with.

    Thanks to all.

  11. #11

    Thread Starter
    Registered User
    Join Date
    Jan 2003
    Posts
    218
    Text file again.
    Attached Files Attached Files

  12. #12
    Member
    Join Date
    Jun 2003
    Location
    Sweden
    Posts
    53
    Hello, i tried to load your file with notepad. and it´s screwed up.. But in textpad it works.. Becuase it can read "unix" text files.. Don´t realy know the diffrent.. But anyway i have a code that fixes the vbCrLf in the file!

    Just get the text in to a textbox then add this text to a button or something! And press it! :-)

    Text1.Text = Replace(txt_Main.Text, Chr(10), vbCrLf, 1, Len(Text1.Text))
    You are reading my signature! :-)

    My WIP site

    Three Great bands:
    Beseech
    Meshuggah
    Opeth

  13. #13

    Thread Starter
    Registered User
    Join Date
    Jan 2003
    Posts
    218
    Ok, thanks. Will try that.

  14. #14

    Thread Starter
    Registered User
    Join Date
    Jan 2003
    Posts
    218
    Yep! That did the trick. I simply have to use chr(10) and not 13 or vbcrlf.

    Thanks to all.

  15. #15

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width