473,406 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

While Not EOF(fn) ... Wend

Baz
Hi All,

I have a problem with the above command. I have a file which is
approximately 7000 lines long. I want to parse that file, extract any
lines containing a certain string, and add that line to a listbox. My
code is roughly as follows:
While Not EOF(1)
Line Input #1, sTemp
If (InStr(1, sTemp, "SEARCH STRING") <> 0) Then
lstList.AddItem sTemp
End If
Wend
When I run this code, everything is fine until about line 4100. After
this, nothing gets added to the list box, even though I can eyeball
the file and see that there are other lines that it should match on.
If I debug.print the line number after the WEND, then I see that is
has broken out at line 4100, and not scanned the rest of the file.

Are there known issues with While...Wend like this? What could cause
the EOF() to return true in the middle of the file? Is there a limit
to the number if lines that can be read like this, for example?

Thanks,

Baz.

Jul 17 '05 #1
6 52957
Baz <ba***********@yahoo.co.uk> schreef in berichtnieuws
8n********************************@4ax.com...
Hi All,
Hello Baz,
I have a problem with the above command. I have a file which is
approximately 7000 lines long. I want to parse that file, extract any
lines containing a certain string, and add that line to a listbox. My
code is roughly as follows:
While Not EOF(1)
Line Input #1, sTemp
If (InStr(1, sTemp, "SEARCH STRING") <> 0) Then
lstList.AddItem sTemp
End If
Wend

When I run this code, everything is fine until about line 4100. After
this, nothing gets added to the list box, even though I can eyeball
the file and see that there are other lines that it should match on.
If I debug.print the line number after the WEND, then I see that is
has broken out at line 4100, and not scanned the rest of the file.

Are there known issues with While...Wend like this? What could cause
the EOF() to return true in the middle of the file? Is there a limit
to the number if lines that can be read like this, for example?


There is about one circumstance that could lead to this and that is a CTRL-Z
(code : 26 dec or 1A hex) in the file itself. This character is considered
an End-Of-File (EOF) marker, and a a file opened in character mode (for
input) will check for it stop when found.

Suggestion : Open the file for binary, and scan it for the above said
character. If found, overwrite it with(for example) a Space, or better yet,
a chr$(254) , which will be visible as a largish square dot.

Regards,
Rudy Wieser

Jul 17 '05 #2
> Are there known issues with While...Wend like this? What could cause
the EOF() to return true in the middle of the file? Is there a limit
to the number if lines that can be read like this, for example?

The known issue is with EOF, it will return True if there is an EOF
character (Chr(26)) somewhere in the file.

Either remove the offending character from the file, or use Binary
access mode to read the file.

To see where in the file you need to check, try:
While Not EOF(1)
Line Input #1, sTemp
Debug.Print Seek(1), Right$(sTemp, 10)
If (InStr(1, sTemp, "SEARCH STRING") <> 0) Then
lstList.AddItem sTemp
End If
Wend

The output to the Immediate window would look something like:

27 roken line
53 aftermath.
111 cut

Where the last number may be way out of sequence. Look to the
line preceeding that for the position in the file where you need to start
looking for an offencing character.

LFS


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 17 '05 #3
Baz
BIG Thanks You's to Rudy and Larry. There indeed was a couple of EOF
characters in the file which, when deleted, allowed me to scan the
file in its entirety.

I guess my next question is this:

Rather than having to manually remove the EOF characters, how can I
programatically get around this problem. Presumably I need to use a
function other than EOF when accessing my file.

I had a look at opening in Binary mode, Larry, but this didn't help me
too much. There is precious little about it in the books I have or any
of the online help sites I referenced.

Thanks,

Baz.

On Wed, 3 Dec 2003 14:19:57 -0600, "Larry Serflaten"
<Ab***@SpamBusters.com> wrote:
Are there known issues with While...Wend like this? What could cause
the EOF() to return true in the middle of the file? Is there a limit
to the number if lines that can be read like this, for example?

The known issue is with EOF, it will return True if there is an EOF
character (Chr(26)) somewhere in the file.

Either remove the offending character from the file, or use Binary
access mode to read the file.

To see where in the file you need to check, try:
While Not EOF(1)
Line Input #1, sTemp
Debug.Print Seek(1), Right$(sTemp, 10)
If (InStr(1, sTemp, "SEARCH STRING") <> 0) Then
lstList.AddItem sTemp
End If
Wend

The output to the Immediate window would look something like:

27 roken line
53 aftermath.
111 cut

Where the last number may be way out of sequence. Look to the
line preceeding that for the position in the file where you need to start
looking for an offencing character.

LFS


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----


Jul 17 '05 #4
Baz <ba***********@yahoo.co.uk> schreef in berichtnieuws
1f********************************@4ax.com...

Hello Baz,
BIG Thanks You's to Rudy and Larry. There indeed was a couple of EOF
characters in the file which, when deleted, allowed me to scan the
file in its entirety.

I guess my next question is this:

Rather than having to manually remove the EOF characters, how can I
programatically get around this problem. Presumably I need to use a
function other than EOF when accessing my file.

I had a look at opening in Binary mode, Larry, but this didn't help me
too much. There is precious little about it in the books I have or any
of the online help sites I referenced.


Just one thing : you will *have* to remove that EOF character, otherwise it
will bite you in the butt somewhere else ...

As I was not sure what the response of a file opened in binary mode to an
(line) input command would be, I just checked :-)

The up-side was that the EOF character is just read as any other character.
The down-side was the the "while not eof(file-handle)" construction did not
work anymore ...

But that's not much of a problem, as you can ask (and get :-) the file's
current read-position as well as it's size, and compare the two.

Below is the result of my "expirimenting" :

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Private Sub Command1_Click()
Dim hFile As Long, i As Integer, sLine As String

hFile = FreeFile
Open sFile For Binary As #hFile
While Seek(hFile) < LOF(hFile)
i = i + 1
Line Input #hFile, sLine
Debug.Print i; sLine
Wend
Close #hFile

End Sub

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

My suggestion would be to allso write a routine that strips a read line from
all control-characters (all characters with ASCII-code below 32, exept maybe
chr$(9), the TAB) ...

Hope this helps (actually, I am allmost *certain* it will :-)

Regards,
Rudy Wieser

Jul 17 '05 #5
"Baz" <ba***********@yahoo.co.uk> wrote

Rather than having to manually remove the EOF characters, how can I
programatically get around this problem. Presumably I need to use a
function other than EOF when accessing my file.

I had a look at opening in Binary mode, Larry, but this didn't help me
too much. There is precious little about it in the books I have or any
of the online help sites I referenced.

See if this helps. I replaced those EOF characters with CrLf thinking
the EOF may have been placed at the end of an earlier file, with more
lines appended after that. You can use a different character if needed.

LFS

Dim data() As Byte, text As String, lines
Dim ff As Long

ff = FreeFile

Open "d:\temp\test.txt" For Binary As ff
ReDim data(1 To LOF(ff))
Get #ff, , data ' Read data
Close ff

text = StrConv(data, vbUnicode) ' Convert to text
text = Replace(text, Chr$(26), vbCrLf) ' Remove EOF characters

lines = Split(text, vbCrLf) ' Convert to array

For ff = LBound(lines) To UBound(lines)
Debug.Print ff, lines(ff) ' Show line
Next

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 17 '05 #6
On Thu, 04 Dec 2003 10:46:36 +0000, Baz <ba***********@yahoo.co.uk>
wrote:
BIG Thanks You's to Rudy and Larry. There indeed was a couple of EOF
characters in the file which, when deleted, allowed me to scan the
file in its entirety.

I guess my next question is this:

Rather than having to manually remove the EOF characters, how can I
programatically get around this problem. Presumably I need to use a
function other than EOF when accessing my file.

I had a look at opening in Binary mode, Larry, but this didn't help me
too much. There is precious little about it in the books I have or any
of the online help sites I referenced.


Here is a VB Class that will give you some ideas

HTH

VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "cReadFileStream"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

' 2/8/01 JF
' 3/8/01 JF - Block Read Added - watch for Block > File Size
'

Private Type TCMN
FileName As String
FileSize As Long
Delin As String
Buffer As String
BufferLen As Long
BufferPos As Long
BytesDone As Long
EofFlag As Boolean
Channel As Integer
End Type

Private cmn As TCMN

' ---
Private Sub Class_Initialize()
cmn.Delin = vbCrLf
cmn.BufferLen = 100000
End Sub

' ---
Public Function Create(FileName$) As Boolean

cmn.FileName = FileName
Create = False
cmn.Buffer = ""
cmn.Channel = 0
cmn.EofFlag = False
cmn.BufferPos = 1
cmn.BytesDone = 0
' ---
If FileExists(FileName$) = False Then
MsgBox "cReadFileStream: " + FileName$ _
+ "File not Found"
Exit Function
End If
' ---
If FileExists(FileName$) Then
cmn.FileSize = FileLen(cmn.FileName)
cmn.Channel = FreeFile
Open FileName For Binary Access Read As #cmn.Channel
Create = True
End If
End Function

' ---
Public Function ReadDelineatedLine() As String
Dim Q&, L&

If cmn.Channel = 0 Then
MsgBox "cReadFileStream - ReadLine - but file not Open"
cmn.EofFlag = True
Exit Function
End If
' ---
If cmn.EofFlag Then
MsgBox "cReadFileStream - Read Past End of File"
Exit Function
End If
' ---
If InStr(cmn.BufferPos, cmn.Buffer, cmn.Delin) = 0 Then
Call LS_FillBuffer
' --- When File completely Read then append Delin if Needed
If cmn.BytesDone = cmn.FileSize Then
If Right$(cmn.Buffer, Len(cmn.Delin)) <> cmn.Delin Then
cmn.Buffer = cmn.Buffer + cmn.Delin
End If
End If
End If

' ---
Q = InStr(cmn.BufferPos, cmn.Buffer, cmn.Delin)
If Q Then
L = Q - cmn.BufferPos
ReadDelineatedLine = Mid$(cmn.Buffer, cmn.BufferPos, L)
cmn.BufferPos = Q + Len(cmn.Delin)
End If
If Q = 0 Then
MsgBox "cReadFileStream - Read - Unexpected Error" _
+ vbCrLf + "Delineator not Found"
End If

' --- Was this the last Field of the Last Buffer
If cmn.BytesDone >= cmn.FileSize Then
If Q >= Len(cmn.Buffer) - Len(cmn.Delin) Then
cmn.EofFlag = True
End If
End If
End Function

' ---
Public Sub ReadBlock(Block$)
Dim BlockLen&, Q&

If cmn.Channel = 0 Then
MsgBox "cReadFileStream - ReadBlock - but file not Open"
cmn.EofFlag = True
Exit Sub
End If
' ---
If cmn.EofFlag Then
MsgBox "cReadFileStream - Read Past End of File"
Exit Sub
End If

' ---
BlockLen& = Len(Block$)

' --- Do we need to fill the Buffer
If (cmn.BufferPos + BlockLen) > Len(cmn.Buffer) Then
If BlockLen > cmn.BufferLen Then ' increase buffer size
cmn.BufferLen = cmn.BufferPos + BlockLen
End If
Call LS_FillBuffer
End If

' --- If insufficient Data left
Q = Len(cmn.Buffer$) - cmn.BufferPos + 1 ' Bytes Left
If BlockLen > Q Then
Block$ = Space$(Q)
BlockLen = Q
End If

' --- Copy the data
Mid$(Block$, 1, BlockLen) = Mid$(cmn.Buffer$, cmn.BufferPos,
BlockLen)
cmn.BufferPos = cmn.BufferPos + BlockLen

' --- Was this the last Field of the Last Buffer
If cmn.BytesDone >= cmn.FileSize Then
If cmn.BufferPos > Len(cmn.Buffer$) Then
cmn.EofFlag = True
End If
End If

End Sub
' ---
Public Function EofFlag() As Boolean
EofFlag = cmn.EofFlag
End Function

' ---
Public Function Size() As Long
Size = cmn.FileSize
End Function

' ---
Public Sub Free()
If cmn.Channel <> 0 Then
Close #cmn.Channel
cmn.Channel = 0
End If
End Sub

' ---
Private Sub LS_FillBuffer()
Dim Hold$, Q&

' --- First time in cmn.Buffer = ""
Hold$ = Mid$(cmn.Buffer, cmn.BufferPos)

If cmn.BytesDone >= cmn.FileSize Then
Exit Sub
End If

' ---
If Len(cmn.Buffer) < cmn.BufferLen Then
cmn.Buffer = Space$(cmn.BufferLen)
End If

' --- Reduce Buffer Size at End of File
Q = cmn.FileSize - cmn.BytesDone
If Q < Len(cmn.Buffer) Then
cmn.Buffer = Space$(Q)
End If

' --- Read a Chunk
Get #cmn.Channel, cmn.BytesDone + 1, cmn.Buffer
cmn.BytesDone = cmn.BytesDone + Len(cmn.Buffer)

' --- Add leftover chunk if needed
If Len(Hold$) Then
cmn.Buffer = Hold + cmn.Buffer
End If
' ---
cmn.BufferPos = 1

End Sub

Private Sub Class_Terminate()
Me.Free
End Sub

'
' Support Routines
'
Function FileExists(Fle$) As Boolean
Dim Q%
On Error Resume Next
Q = GetAttr(Fle$)
If Err = 0 Then
If (Q And vbDirectory) = 0 Then
FileExists = True
End If
End If
Err.Clear
End Function
Jul 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Thomas Guettler | last post by:
Hi, Simple excerise: You have a sorted list of integers: l= and you should fill all gaps: result ==
7
by: Raphael Gluck | last post by:
Hi I've been trying to hand code my pages, after failing in a WSYWYG editor and giving up. I'm almost done with the page checking, it's just i'm having a Wend error Microsoft VBScript...
2
by: brentster | last post by:
I am trying to write a stored procedure that utilizes something like a while in ASP. my select statement is like this. s = select distinct(employee_id) from employee_table rs.Open s while...
2
by: Alexey.Murin | last post by:
The application we are developing uses MS Access 2003 database (with help of ADO). We have noticed that during massive records updating the size of the mdb file increases dramatically (from 3-4 to...
1
by: pauljturner99 | last post by:
Hi, I'm trying to pass a parameter from a for loop to the nested while loop but only the first counter is passed. Here is the code: dim ctr redim ctr(5) ctr(0) = 2 ctr(1) = 4 ctr(2) = 6
7
by: ruvi | last post by:
I am getting runtime error 3021 - Either EOF or BOF is true or the current record has been deleted..... I have 2 combo boxes in a form- One for the client and the other for the project. When the...
0
by: Marc Vangrieken | last post by:
Hi I have a really weird problem... I'm using some .NET assemblies in classic ASP pages (VBScript) through COM. To make it short; i have some methods and they all return an instance of...
4
idsanjeev
by: idsanjeev | last post by:
hello i am using it for diaplay massage if selected cartidge color is not color the massage oherwise work when remove the while loop it accept first color of that cartidge. Prn = VAR_TYPE ...
1
by: Octo Siburian | last post by:
I have error message when i run this script QuerySelect = "SELECT a.DN, b.DIN, c.PIN, " & _ "c.UserName, b.Clock, d.ItemName " & _ "FROM ((ras_Device a INNER JOIN...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.