|
-
Sep 28th, 2002, 03:38 PM
#1
Thread Starter
Registered User
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.
-
Sep 28th, 2002, 04:40 PM
#2
PowerPoster
Here sis how to:
VB Code:
Private Sub Form_Load()
'=======================
Dim strLine As String
On Error Resume Next
Open "c:\test.txt" For Input As #1
Do
Line Input #1, strLine
List1.AddItem strLine
Loop Until EOF(1)
Close #1
End Sub
-
Sep 28th, 2002, 04:59 PM
#3
Addicted Member
I think she wants to input it one line at a time when pressing a commandbutton, this would do the job
VB Code:
Private Sub form_load()
Open "yourfile" For Input As #1
End Sub
Private Sub Command1_Click()
If Not EOF(1) Then Input #1, Var
If EOF(1) Then
Close #1
Command1.Enabled = False
End If
Text1.Text = Var
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.
-
Sep 28th, 2002, 05:12 PM
#4
PowerPoster
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:
Private Sub Command1_Click()
'=======================
Dim strLine As String
On Error Resume Next
Open "c:\test.txt" For Input As #1
Do
Line Input #1, strLine
List1.AddItem strLine
Loop Until EOF(1)
Close #1
End Sub
-
Sep 28th, 2002, 05:18 PM
#5
Junior Member
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:
Private Sub form_load()
Open "yourfile" For Input As #1
End Sub
Private Sub Command1_Click()
Dim Var As String
If Not EOF(1) Then Line Input #1, Var
If EOF(1) Then
Close #1
Command1.Enabled = False
End If
Text1.Text = Var
End Sub
I hope it helps
-
Sep 28th, 2002, 05:22 PM
#6
Junior Member
IROY55...
Usually isn't how files is processed, but it works!!! I did it several times and never have any trouble about this....
-
Sep 28th, 2002, 05:30 PM
#7
PowerPoster
If that's how you usually do things then I would horrified if I ever had to support anything you've ever developed.
-
Sep 28th, 2002, 05:31 PM
#8
Addicted Member
This is an option that will keep IROY happy too
VB Code:
'Declarations:
Dim i As Integer
Private Sub Form_Load()
i = 1
End Sub
Private Sub Command1_Click()
Dim Var As String
Open "yourfile" For Input As #1
For a = 1 To i
If Not EOF(1) Then Line Input #1, Var
If EOF(1) Then
Command1.Enabled = False
End If
Next a
Close #1
Text1.Text = Var
i = i + 1
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
-
Sep 28th, 2002, 05:41 PM
#9
PowerPoster
Much much cleaner. Bravo.
-
Sep 28th, 2002, 06:11 PM
#10
The picture isn't missing
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  .
-
Sep 28th, 2002, 08:01 PM
#11
PowerPoster
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.
-
Sep 28th, 2002, 08:04 PM
#12
The picture isn't missing
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  .
-
Sep 29th, 2002, 06:24 AM
#13
Thread Starter
Registered User
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.
-
Sep 29th, 2002, 06:31 AM
#14
Not NoteMe
VB Code:
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
[b]Loop Until EOF([u]2[/u])[/b] 'I think
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
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. 
-
Sep 29th, 2002, 06:38 AM
#15
Thread Starter
Registered User
I made that change, but now it only shows the list line in the textbox?
-
Sep 29th, 2002, 06:45 AM
#16
Not NoteMe
VB Code:
Private Sub Command1_Click()
Dim strLine As String
On Error Resume Next
Open "batch.txt" For Input As #2
Do
Line Input #2, strLine
[b]Text1.Text = Text1.Text & CrLf & strLine[/B] 'can't believe i missed this!
Loop Until EOF(2)
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
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. 
-
Sep 29th, 2002, 08:02 AM
#17
Thread Starter
Registered User
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.
-
Sep 29th, 2002, 08:29 AM
#18
Not NoteMe
VB Code:
Private Sub Command1_Click()
Dim strLine As String
On Error Resume Next
If EOF(2) Then
Close #2
Msgbox "No More Lines"
Else
Line Input #2, strLine
Text1.Text = Text1.Text & CrLf & strLine
End If
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. 
-
Sep 29th, 2002, 08:55 AM
#19
Thread Starter
Registered User
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
-
Sep 29th, 2002, 01:19 PM
#20
Thread Starter
Registered User
Anyone
-
Sep 29th, 2002, 01:36 PM
#21
Not NoteMe
Sorry, a mistake on my part.
VB Code:
Dim FirstClick As Boolean
'~~~~~~~~
'~~~~~~~~
Private Sub Form_Load()
FirstClick = False
End Sub
Private Sub Command1_Click()
Dim strLine As String
If FirstClick = False Then
Open "batch.txt" For Input As #2
FirstClick = [b]True[/b] 'OOps!
End If
On Error Resume Next
If EOF(2) Then
Close #2
Msgbox "No More Lines"
Else
Line Input #2, strLine
Text1.Text = Text1.Text & CrLf & strLine
End If
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. 
-
Sep 29th, 2002, 01:43 PM
#22
Fanatic Member
Originally posted by LucianoBraatz
Hi Jenny.............
VB Code:
Private Sub form_load()
Open "yourfile" For Input As #1
End Sub
Private Sub Command1_Click()
Dim Var As String
If Not EOF(1) Then Line Input #1, Var
If EOF(1) Then
Close #1
Command1.Enabled = False
End If
Text1.Text = Var
End Sub
I hope it helps
You forgot to close the file
VB Code:
Private Sub Form_Unload
Close #1
End Sub
Kinjal
Last edited by kinjalgp; Sep 30th, 2002 at 07:42 AM.
-
Sep 29th, 2002, 01:45 PM
#23
Thread Starter
Registered User
It keeps saying "file already open"
-
Sep 29th, 2002, 01:48 PM
#24
Fanatic Member
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
-
Sep 29th, 2002, 01:57 PM
#25
Not NoteMe
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. 
-
Sep 29th, 2002, 05:07 PM
#26
Thread Starter
Registered User
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
-
Sep 29th, 2002, 05:25 PM
#27
Thread Starter
Registered User
Thanx for all the help and support
What would I do without you guys
-
Sep 29th, 2002, 05:35 PM
#28
Hyperactive Member
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 
-
Sep 29th, 2002, 10:17 PM
#29
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|