Results 1 to 7 of 7

Thread: [Resolved] Multiline File into a single line string.

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    [Resolved] Multiline File into a single line string.

    This problem always keep comming up. When reading from a file that has multiple lines during debug (though i only need the first line), the program works fine. But when compiled and trying to read from it, the it somtimes reads the first line as null and i have to make code that will read the second line. In notepad it says it's the first line.

    VB Code:
    1. Open Filename For Input As #1
    2. Line Input #1, File_Contents
    3. Close #1

    ^^^^^ Code used.


    Any way, what i want to know is how to load the entire file contents into a string. No matter how many lines or what ever else.

    If i had this stored in a file:

    QWERTY
    ASDFGH
    ZXCVBN

    and loaded it, i want it to look like this in a string "QWERTYASDFGHZXCVBN"

    Can anyone help me out?
    Last edited by Slyke; Jan 23rd, 2007 at 10:13 AM.

  2. #2
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Multiline File into a single line string.

    Dim strStream As String

    Open Filename For Input As #filenumber
    strStream = Input(LOF(filenumber), #filenumber)
    strStream = Replace(strStream, Chr(0), "")
    strStream = Replace(strStream, vbCrLF, "")

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: Multiline File into a single line string.

    Thanks dude!

    VB Code:
    1. Dim strStream As String
    2.  
    3. Open Filename For Input As #1
    4. strStream = Input(LOF(1), #1)
    5. strStream = Replace(strStream, Chr(0), "")
    6. strStream = Replace(strStream, vbCrLF, "")
    7. Close #1


    This correct?

  4. #4
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: Multiline File into a single line string.

    VB Code:
    1. Private Sub Command1_Click()
    2. Dim filename As String
    3. filename = App.Path & "\List.txt"
    4. Open filename For Input As #1
    5. Text1.Text = Input(LOF(1), #1)
    6. Text1.Text = Replace(Text1.Text, Chr(0), "")
    7. Text1.Text = Replace(Text1.Text, vbCrLf, "")
    8. Close #1
    9. End Sub

    I done this and it worked perfectly no errors well unless the file isn't their but im sure you would know what to do then

  5. #5
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: Multiline File into a single line string.

    VB Code:
    1. Private Sub Command1_Click()
    2. Dim filename As String
    3. filename = App.Path & "\Listview.txt"
    4. Open filename For Input As #1
    5. Text1.Text = Input(LOF(1), #1)
    6. Text1.Text = Replace(Text1.Text, Chr(0), "")
    7. Text1.Text = Replace(Text1.Text, vbCrLf, "")
    8. Text1.Text = Replace(Text1.Text, Space$(number here), "")
    9. Close #1
    10. End Sub

    This will put spaces in between each line. Set the integer to what you want

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: Multiline File into a single line string.

    Cool, thanks for this!

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

    Re: [Resolved] Multiline File into a single line string.

    Thanks for letting us know that you have your answer. The next time you may find it easier by just pulling down the Thread Tools menu and clicking the Mark Thread Resolved button.

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