Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

open one line from a file

Status
Not open for further replies.

ktucci

Programmer
Apr 23, 2001
146
US
i need to open a file as a string...but the file is > 2 mil characters...is there a way i can open 1 line at a time

the code below works for small files...i just need to return 1 line as string at a time....any help will be greatly appreciated..thanks

keith

Private Sub Command2_Click()
datstring = OpenTextFile("dat3.dat")
getstring1$ = datstring
Text1.Text = Str(FileLen("dat3.dat"))
Do
On Error GoTo errorhandler
tempstring$ = InStr(getstring1$, "/TN")
If tempstring$ = "0" Then
Exit Do
End If
getstring1$ = Mid(getstring1$, InStr(getstring1$, "/TN") + 6)
getstring2$ = getstring2$ & Left(getstring1$, 12) & "|"
Loop
errorhandler:
Call SaveTextFile("test.txt", getstring2$)
MsgBox "DONE"
End Sub
 
Can't u just open the file input and use the Line Input statement. This will read one line in at a time, and just loop until eof
 
You could read the file in blocks of, say, 10k. However, you're doing a search and replace (?) so if your search string happened to fall right at the end of a block you'd not get a match, so you need to cater for that too.

EG:

a% = FreeFile
Open "file.dat" For Binary Access Read As a%
Do While Not EOF(a%)
s% = 10240
if ((LOF(a%) - Seek(a%)) < s%) then
s% = LOF(a%) - Seek(a%)
end if
N$ = Space$(s%)
Get #a%, , N$
N$ = LastBitOfN$ & N$

' Do work on N$ here...
'

' Store the end of the last block read...
LastBitOfN$ = Right$(N$, 50)
Loop

Close a%

- Andy.
 
this is the solution i have come up with...

Function OpenTextFile1(xPath As String, sPath As String) As String
Dim a As Integer
Dim fileNum As Integer, fileBuffer As String, tempBuffer As String
If Exists(xPath) = False Then OpenTextFile1 = &quot;BAD PATH&quot;: Exit Function
Form1.Label2.Caption = Str(FileLen(xPath))
fileNum = FreeFile()
Open xPath For Input As #fileNum
Do While Not EOF(fileNum): DoEvents
Line Input #fileNum, getstring1$
Do
Form1.Label1.Caption = a + 1
'On Error GoTo errorhandler
tempstring$ = InStr(getstring1$, &quot;/TN&quot;)
If tempstring$ = &quot;0&quot; Then
Exit Do
End If
getstring1$ = Mid(getstring1$, InStr(getstring1$, &quot;/TN&quot;) + 6)
getstring2$ = getstring2$ & Left(getstring1$, 12) & &quot;|&quot;
a = a + 1
Loop
'errorhandler:
Call SaveTextFile(&quot;test.txt&quot;, getstring2$)
Loop
Close #fileNum
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top