Results 1 to 19 of 19

Thread: Reading In a Text File One Line At a Time

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2004
    Location
    Belfast
    Posts
    116

    Reading In a Text File One Line At a Time

    I am trying to read in one line of a text file at a time, basically, the text file will be full of numbers, but i want to read in one line at a time and display it in a text box, i can read in a full text file in one go but i dont know how to read in one line at a time, here is the code that reads in the whole text file, any help would be great, thanks....

    'code:

    Dim line As String
    Open Filename For Input As #1
    Do While Not EOF(1)
    Input #1, line
    Text1.Text = Text1.Text & vbCrLf & line
    DoEvents
    Loop
    Close #1

  2. #2
    Fanatic Member ahara's Avatar
    Join Date
    Nov 2003
    Location
    Toronto
    Posts
    531
    I must be missing something (???) If you lose the loop, you can input a line. Is that what you want? (ps - you do realize that you share your name with one of the most famous Canadians ever )
    "Knowledge is gained when different people look at the same information in different ways"

    - Louis Pasteur

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2004
    Location
    Belfast
    Posts
    116
    im not really sure, the text file looks like

    254
    147
    587
    84
    64
    etc etc.....
    i want to read in one line at a time, is that possible without using excel???

  4. #4
    Frenzied Member
    Join Date
    May 2003
    Location
    So Cal
    Posts
    1,564
    Turn on MultiLine for your text box and use this:

    Line Input #1, line

    instead of

    Input #1, line

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Apr 2004
    Location
    Belfast
    Posts
    116
    thanks i will give it a go

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Apr 2004
    Location
    Belfast
    Posts
    116
    sorry mate it didnt work, still gives the smae result, appreciate the help

    so can anyone else help me or am i doomed forever, and who is this famous Canadian with the same name as me, must be a good lookin guy!!!!!

  7. #7
    Frenzied Member
    Join Date
    May 2003
    Location
    So Cal
    Posts
    1,564
    Sure it does, use Debug and Step Into the code and read your Line string on each pass.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Apr 2004
    Location
    Belfast
    Posts
    116
    yeah, but what do u do when u compile the programme and havent got the option to debug and step???

  9. #9
    Frenzied Member
    Join Date
    May 2003
    Location
    So Cal
    Posts
    1,564
    Not sure what you mean.. the code pulls in 1 line at a time just like you asked. You need to decide what to do with each line as it comes in.

    Why not explain what your goal is?

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Apr 2004
    Location
    Belfast
    Posts
    116
    i have tried what you suggested but it simply pulls in the entire text of the file when i click on my import button, i cant get it to jsut simply read in one line at a time

    i.e if the text files looks like this:

    245
    547
    874
    854
    ...etc etc....
    when i click on import i want my text box to look like:

    245

    then when i cick the import button again i want the text box to look like:

    245
    547

    and so on untill all lines have been read in, is this possible?

  11. #11
    Fanatic Member ahara's Avatar
    Join Date
    Nov 2003
    Location
    Toronto
    Posts
    531
    I played around with a test project - it is attached...hope it helps (it does import one line at a time)....Paul Henderson scored the winning goal in the first series between Canada and Russia in 1972....as for his or your looks, I honestly cannot say
    Attached Files Attached Files
    "Knowledge is gained when different people look at the same information in different ways"

    - Louis Pasteur

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Apr 2004
    Location
    Belfast
    Posts
    116
    cheers mate thats great, just one question, i know i sound thick right now but how do i make it so that the programme can choose which text file it imports??? i have included what i have done so far, this one lets u choose while text file but how do i merge ur code and my code together!!!!

  13. #13
    Frenzied Member
    Join Date
    May 2003
    Location
    So Cal
    Posts
    1,564
    I didnt look at the other code posted, but this is what I did real fast just to give you an idea

    VB Code:
    1. Option Explicit
    2.  
    3. Dim line As String
    4. Dim filename As String
    5. Dim myarray() As String
    6. Dim strTemp As String
    7. Dim x As Integer
    8.  
    9.  
    10. Private Sub Command1_Click()
    11. If x = UBound(myarray) + 1 Then
    12.     MsgBox "Last Record was read"
    13. Else
    14.     Text1.Text = Text1.Text & vbCrLf & myarray(x)
    15.     x = x + 1
    16. End If
    17. End Sub
    18.  
    19. Private Sub Form_Load()
    20.  
    21. x = 1
    22. filename = "C:\test.txt"
    23.  
    24. Open filename For Input As #1
    25. Do While Not EOF(1)
    26.     Input #1, line
    27.     strTemp = strTemp & "," & line
    28.     DoEvents
    29. Loop
    30. Close #1
    31.  
    32. myarray = Split(strTemp, ",")
    33.  
    34. End Sub

  14. #14
    Junior Member
    Join Date
    Apr 2004
    Posts
    24
    I read this post and find it is useful. However, I experienced that if the text file contains commas, it will be omitted while displaying in the textbox. Any comments and idea? Thanks

  15. #15
    Frenzied Member
    Join Date
    May 2003
    Location
    So Cal
    Posts
    1,564
    If the comma is part of the string it will be displayed. If you use it as a delimiter as I did in my example and split the string into an array, then it will be stripped.

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Apr 2004
    Location
    Belfast
    Posts
    116
    cheers mate i will give it a go, is there any way it can be simply modified to allow you to choose which text fileu want to use, ie a file open button?

  17. #17
    Frenzied Member
    Join Date
    May 2003
    Location
    So Cal
    Posts
    1,564
    Sure can, you can use a Text box on your form for them to enter a path/filename or use the CommonDialog component to grab the filename.

  18. #18
    Fanatic Member ahara's Avatar
    Join Date
    Nov 2003
    Location
    Toronto
    Posts
    531
    I'll be honest - I have not used the input statement since school....my work rarely involves the use of flat files...just did a quick check and yeah the delimiter for the input statement is a comma by default. I suppose you could read the file in and use replace function to clean it up if worst came to worst, but the advantage is it allows you to read in fields using multiple variables:

    Code:
    Input #iFileNum, strField1, strField2, strField3
    If the file above had three fields separated by commas, you could read each line into its respective variable. But since henderson's need is only one column, it should still work out for him.
    "Knowledge is gained when different people look at the same information in different ways"

    - Louis Pasteur

  19. #19

    Thread Starter
    Lively Member
    Join Date
    Apr 2004
    Location
    Belfast
    Posts
    116
    cheers you guys have been great, ill give u a bit of backround as to why in need this and also why i am so confused. I hav e been given this assignment from a lecturer at uni, he wants us to read in a .wav file in text format, i used to think i knew a fair bit about computers music mp3's wav files and all that stuff, obviously not, i didnt know u could read in a .wav file as text, then he wants teh values in the text file, each line to be multiplied by 256 and add on the next line... still following, im not.... then these answers are displayed in another label or text box, presumably to give a list of values to form a spectrum analyser..... do any of you infinetley beings have a clue what im talking about????


    Sincerely mad and dillusional

    Paul Henderson

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