Results 1 to 29 of 29

Thread: 1 line at a time ::RESOLVED::

  1. #1

    Thread Starter
    Registered User
    Join Date
    Sep 2002
    Posts
    221

    Unhappy 1 line at a time ::RESOLVED::

    Hi

    Can anyone show me how to get a line out from a text file 1 at a time for processing.

    For example, This text file can have a variable number of lines. I want to be able to press a commandbutton which gets the first line from the text file and displays it in a textbox.

    If I press the commandbutton a second time, the program gets the second line out and displays it the same way as above.

    Then the third line and so on until it reaches the end and stops.

    I think the EOF will need to be used.

    please help
    Last edited by jennysmith; Sep 29th, 2002 at 05:21 PM.

  2. #2
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    Here sis how to:
    VB Code:
    1. Private Sub Form_Load()
    2. '=======================
    3. Dim strLine As String
    4.  
    5. On Error Resume Next
    6.  
    7.     Open "c:\test.txt" For Input As #1
    8.         Do
    9.             Line Input #1, strLine
    10.             List1.AddItem strLine
    11.         Loop Until EOF(1)
    12.     Close #1
    13.  
    14. End Sub
    Roy

  3. #3
    Addicted Member glyptar's Avatar
    Join Date
    Sep 2002
    Location
    The Netherlands
    Posts
    138
    I think she wants to input it one line at a time when pressing a commandbutton, this would do the job

    VB Code:
    1. Private Sub form_load()
    2.     Open "yourfile" For Input As #1
    3. End Sub
    4.  
    5. Private Sub Command1_Click()
    6.     If Not EOF(1) Then Input #1, Var
    7.     If EOF(1) Then
    8.         Close #1
    9.         Command1.Enabled = False
    10.     End If
    11.     Text1.Text = Var
    12. End Sub

    i made it close the file and disable the command button when you reach end of file, you can do something else there if you want.
    Glyptar

  4. #4
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    glyptar,
    this NOT how file is been processed usually: instead opning a file in one place and close in some other - you should do this whithin one same procedure. Besides, what the heck is the difference? If she wants in the button click event then she must be capable of doing such thing as copy/paste code from one place to another:
    VB Code:
    1. Private Sub Command1_Click()
    2. '=======================
    3. Dim strLine As String
    4.  
    5. On Error Resume Next
    6.  
    7.     Open "c:\test.txt" For Input As #1
    8.         Do
    9.             Line Input #1, strLine
    10.             List1.AddItem strLine
    11.         Loop Until EOF(1)
    12.     Close #1
    13.  
    14. End Sub
    Roy

  5. #5
    Junior Member
    Join Date
    Aug 2002
    Location
    Brazil
    Posts
    29
    Hi Jenny...

    The reply from gliptar its almost what you want... If you are attempting to read a text file the better is use Line Input instead Input because the Input looks the variable's type and how its was undefined, it will be considered as Variant so the VB will take the firsts bytes from the line to figure out which type is the data in the file. If you use Line Input the VB stops storing bytes in the variable when it finds the Carriage Return and Line Feed characters or the end of file.
    I think this code is more appropriated to you...
    VB Code:
    1. Private Sub form_load()
    2.     Open "yourfile" For Input As #1
    3. End Sub
    4.  
    5. Private Sub Command1_Click()
    6.     Dim Var As String
    7.     If Not EOF(1) Then Line Input #1, Var
    8.     If EOF(1) Then
    9.         Close #1
    10.         Command1.Enabled = False
    11.     End If
    12.     Text1.Text = Var
    13. End Sub

    I hope it helps

  6. #6
    Junior Member
    Join Date
    Aug 2002
    Location
    Brazil
    Posts
    29
    IROY55...
    Usually isn't how files is processed, but it works!!! I did it several times and never have any trouble about this....

  7. #7
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    If that's how you usually do things then I would horrified if I ever had to support anything you've ever developed.
    Roy

  8. #8
    Addicted Member glyptar's Avatar
    Join Date
    Sep 2002
    Location
    The Netherlands
    Posts
    138
    This is an option that will keep IROY happy too

    VB Code:
    1. 'Declarations:
    2.  
    3. Dim i As Integer
    4.  
    5. Private Sub Form_Load()
    6.     i = 1
    7. End Sub
    8.  
    9. Private Sub Command1_Click()
    10.     Dim Var As String
    11.     Open "yourfile" For Input As #1
    12.     For a = 1 To i
    13.         If Not EOF(1) Then Line Input #1, Var
    14.         If EOF(1) Then
    15.             Command1.Enabled = False
    16.         End If
    17.     Next a
    18.     Close #1
    19.     Text1.Text = Var
    20.     i = i + 1
    21. End Sub

    opens and closes the file in the same sub and works fine, reading one line at a time (which is what she asked for).
    Last edited by glyptar; Sep 28th, 2002 at 05:35 PM.
    Glyptar

  9. #9
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    Much much cleaner. Bravo.
    Roy

  10. #10
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    Originally posted by IROY55
    Much much cleaner. Bravo.
    other than the fact that it is much unecessary to loop through the file again everytime to get a new line.

    why not just read the whole file and store it in a public array variable, then call the array everytime to get the line?

    'in a module
    Public arrFile() As String

    'In where ever
    dim a as string
    open "C:\hi.txt" for input as #1
    a = input$(LOF(1),1)
    arrFile = Split(a,vbcrlf)
    close #1

    'and this where ever you want to get the line:
    LineNumber = 20
    MsgBox ArrFile(LineNumber + 1)
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  11. #11
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    I would agree on that, however Jenny is a beginner so it would be much easier for her to learn one technic at the time. When someone's asking something like "Hey, guys! Can you help me to optimize this ...?" In this case - of course, every idea is quite welcome. But in Jenny's case - the simpler the better.
    Roy

  12. #12
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    simplicity is in the eye of the beholder. maybe both are simple. who knows?
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  13. #13

    Thread Starter
    Registered User
    Join Date
    Sep 2002
    Posts
    221
    Thanx for all the help guys

    This is what I have put together so far: (with your help)

    Private Sub Command1_Click()

    Dim strLine As String

    On Error Resume Next

    Open "batch.txt" For Input As #2
    Do
    Line Input #2, strLine
    Text1.Text = strLine
    Loop Until EOF(1)

    Close #2

    End Sub

    Private Sub Form_Load()

    Open "batch.txt" For Output As #1
    Close #1
    'Deletes the batch.txt file
    Kill "batch.txt"

    End Sub

    Private Sub List1_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)

    Dim Files As Variant
    Dim intFileNum As Integer

    'intFileNum = FreeFile

    'Opens batch.txt for modifying
    Open App.Path & "\batch.txt" For Append As #3

    'Add the drapped file info the batch.txt and on listbox1
    For Each Files In Data.Files
    List1.AddItem Files
    Print #3, Files
    Next

    'Closed the batch.txt file
    Close #3

    Effect = vbDropEffectNone
    End Sub

    The only problem is, when I click the command button, it shows the first line from the text file, if I press the button a second time it does not show the second line in the text file, instead keeps showing the first line?

    Whats going on???
    Last edited by jennysmith; Sep 29th, 2002 at 06:29 AM.

  14. #14
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    VB Code:
    1. Private Sub Command1_Click()
    2.  
    3. Dim strLine As String
    4.  
    5. On Error Resume Next
    6.  
    7. Open "batch.txt" For Input As #2
    8. Do
    9.     Line Input #2, strLine
    10.     Text1.Text = strLine
    11. [b]Loop Until EOF([u]2[/u])[/b] 'I think
    12.  
    13. Close #2
    14.  
    15. End Sub
    16.  
    17. Private Sub Form_Load()
    18.  
    19. Open "batch.txt" For Output As #1
    20. Close #1
    21. 'Deletes the batch.txt file
    22. Kill "batch.txt"
    23.  
    24. End Sub
    25.  
    26. Private Sub List1_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
    27.  
    28. Dim Files As Variant
    29. Dim intFileNum As Integer
    30.  
    31. 'intFileNum = FreeFile
    32.  
    33. 'Opens batch.txt for modifying
    34. Open App.Path & "\batch.txt" For Append As #3
    35.  
    36. 'Add the drapped file info the batch.txt and on listbox1
    37. For Each Files In Data.Files
    38.     List1.AddItem Files
    39.     Print #3, Files
    40. Next
    41.  
    42. 'Closed the batch.txt file
    43. Close #3
    44.  
    45. Effect = vbDropEffectNone
    46. End Sub
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  15. #15

    Thread Starter
    Registered User
    Join Date
    Sep 2002
    Posts
    221
    I made that change, but now it only shows the list line in the textbox?

  16. #16
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    VB Code:
    1. Private Sub Command1_Click()
    2.  
    3. Dim strLine As String
    4.  
    5. On Error Resume Next
    6.  
    7. Open "batch.txt" For Input As #2
    8. Do
    9.     Line Input #2, strLine
    10.     [b]Text1.Text = Text1.Text & CrLf & strLine[/B] 'can't believe i missed this!
    11. Loop Until EOF(2)
    12. Close #2
    13.  
    14. End Sub
    15.  
    16. Private Sub Form_Load()
    17.  
    18. Open "batch.txt" For Output As #1
    19. Close #1
    20. 'Deletes the batch.txt file
    21. Kill "batch.txt"
    22.  
    23. End Sub
    24.  
    25. Private Sub List1_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
    26.  
    27. Dim Files As Variant
    28. Dim intFileNum As Integer
    29.  
    30. 'intFileNum = FreeFile
    31.  
    32. 'Opens batch.txt for modifying
    33. Open App.Path & "\batch.txt" For Append As #3
    34.  
    35. 'Add the drapped file info the batch.txt and on listbox1
    36. For Each Files In Data.Files
    37.     List1.AddItem Files
    38.     Print #3, Files
    39. Next
    40.  
    41. 'Closed the batch.txt file
    42. Close #3
    43.  
    44. Effect = vbDropEffectNone
    45. End Sub
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  17. #17

    Thread Starter
    Registered User
    Join Date
    Sep 2002
    Posts
    221
    Now it shows all lines at the same time, e.g c:\music.mp3c:\documents and settings\letter.docc:\invoice.doc

    I want it to display from the text file:

    line 1 on the first click (only the first line from the text file, even if it has 100 lines!)
    line 2 on the second click (only the second line, not line1 and line2 and 3....)
    line 3 on the third click (only the third line, not line2 and line3 and 4....)

    and so on until the end of the file has been reached

    e.g. 1 line at a time

    I do appreciate the help
    Last edited by jennysmith; Sep 29th, 2002 at 08:37 AM.

  18. #18
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim strLine As String
    3.     On Error Resume Next
    4.     If EOF(2) Then
    5.         Close #2
    6.         Msgbox "No More Lines"
    7.     Else
    8.         Line Input #2, strLine
    9.         Text1.Text = Text1.Text & CrLf & strLine
    10.     End If
    11. End Sub
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  19. #19

    Thread Starter
    Registered User
    Join Date
    Sep 2002
    Posts
    221
    Thanx for the reply SLH

    I tried out your code, but doesnt work. The 1st 1 displayed all the files from the text file at the same time, and the second 1 displayed a message as soon as the button is pressed.

    Im trying to get this program to read the text file which is created when files are dragged into the listbox. The number of lines in the text file depends on the number of files dragged into it. This part works perfectly.

    The commandbutton does not work. This is want Im trying to get the commandbutton to do when its pressed:

    press 1 - read only the first line in the text file and display it in the textbox

    press 2 - read only the second line in the text file and display it in the text box

    press 3 - read only the third line in the text file and display it in the text box.

    press 4, press 5 ...... and so on

  20. #20

    Thread Starter
    Registered User
    Join Date
    Sep 2002
    Posts
    221
    Anyone

  21. #21
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    Sorry, a mistake on my part.

    VB Code:
    1. Dim FirstClick As Boolean
    2.  
    3. '~~~~~~~~
    4. '~~~~~~~~
    5. Private Sub Form_Load()
    6.     FirstClick = False
    7. End Sub
    8. Private Sub Command1_Click()
    9.     Dim strLine As String
    10.     If FirstClick = False Then
    11.         Open "batch.txt" For Input As #2
    12.         FirstClick = [b]True[/b] 'OOps!
    13.     End If
    14.     On Error Resume Next
    15.     If EOF(2) Then
    16.         Close #2
    17.         Msgbox "No More Lines"
    18.     Else
    19.         Line Input #2, strLine
    20.         Text1.Text = Text1.Text & CrLf & strLine
    21.     End If
    22. End Sub
    Last edited by SLH; Sep 29th, 2002 at 01:55 PM.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  22. #22
    Fanatic Member kinjalgp's Avatar
    Join Date
    Apr 2000
    Location
    India
    Posts
    535
    Originally posted by LucianoBraatz
    Hi Jenny.............

    VB Code:
    1. Private Sub form_load()
    2.     Open "yourfile" For Input As #1
    3. End Sub
    4.  
    5. Private Sub Command1_Click()
    6.     Dim Var As String
    7.     If Not EOF(1) Then Line Input #1, Var
    8.     If EOF(1) Then
    9.         Close #1
    10.         Command1.Enabled = False
    11.     End If
    12.     Text1.Text = Var
    13. End Sub

    I hope it helps
    You forgot to close the file
    VB Code:
    1. Private Sub Form_Unload
    2.         Close #1
    3. End Sub

    Kinjal
    Last edited by kinjalgp; Sep 30th, 2002 at 07:42 AM.

  23. #23

    Thread Starter
    Registered User
    Join Date
    Sep 2002
    Posts
    221
    It keeps saying "file already open"

  24. #24
    Fanatic Member kinjalgp's Avatar
    Join Date
    Apr 2000
    Location
    India
    Posts
    535
    Yes It will say like that bcos u have not closed that file during your last execution and it will remain open until u restart your system. So make sure everytime u open the file u close it using "Close #FileNumber" statement

    Kinjal

  25. #25
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    Originally posted by jennysmith
    It keeps saying "file already open"
    I've edited my post above.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  26. #26

    Thread Starter
    Registered User
    Join Date
    Sep 2002
    Posts
    221
    It still doesnt work, will only display the first line from the textfile in the textbox. will not display 2nd line in textbox on its own on 2nd press. will not display 3rd line in textbox on its own on 3rd press etc

  27. #27

    Thread Starter
    Registered User
    Join Date
    Sep 2002
    Posts
    221
    Thanx for all the help and support

    What would I do without you guys

  28. #28
    Hyperactive Member Wak's Avatar
    Join Date
    Nov 2000
    Location
    Brisbane, Queensland
    Posts
    298

    This might be a late reply

    This could be a late reply, but i think this is what you want???

    Button:Command1
    ListBox:Listbox1

    Code:
    Option Explicit
    
    Private Sub Command1_Click()
        Static i As Integer, j As Integer, strData As String
        j = j + 1
        Open "C:\YourTextFile.txt" For Input As #1
            For i = 1 To j
                Line Input #1, strData
            Next i
            List1.AddItem strData
        Close #1
    End Sub
    The first time you click it will only add the first line of the text file to the list box. On the 2nd time you click the button it will only add the 2nd line of the file to the list box (the first line from the text file is already there from the first time you clicked??)

    If u need more help, don't hesitate...
    Visual Basic 6.0 Enterprise
    Visual C++ 6.0 Professional

    Wak

  29. #29
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Exclamation

    as a side note, instead of telling vb which file handle to use, use freefile to find an available one and store it in a variable then use that one to open/close. it will save headaches in the long run on large programs.
    side note #2:
    If you use CLOSE without a number after it, it closes all file handles.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

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