Go Back   Forum Care Forums > Development Reference Area > Visual Basic

Reply
 
LinkBack Thread Tools Display Modes
While Not EOF(fn) ... Wend
Old
  (#1)
Baz
Guest
 
Posts: n/a
Default While Not EOF(fn) ... Wend - 06-04-2007, 08:54 AM

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.

   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Re: While Not EOF(fn) ... Wend
Old
  (#2)
R.Wieser
Guest
 
Posts: n/a
Default Re: While Not EOF(fn) ... Wend - 06-04-2007, 08:54 AM

Baz <EMAIL REMOVED> schreef in berichtnieuws
EMAIL REMOVED...
> 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



   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: While Not EOF(fn) ... Wend
Old
  (#3)
Larry Serflaten
Guest
 
Posts: n/a
Default Re: While Not EOF(fn) ... Wend - 06-04-2007, 08:54 AM

> 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! =-----
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: While Not EOF(fn) ... Wend
Old
  (#4)
Baz
Guest
 
Posts: n/a
Default Re: While Not EOF(fn) ... Wend - 06-04-2007, 08:54 AM

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"
<EMAIL REMOVED> 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! =-----


   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: While Not EOF(fn) ... Wend
Old
  (#5)
R.Wieser
Guest
 
Posts: n/a
Default Re: While Not EOF(fn) ... Wend - 06-04-2007, 08:54 AM

Baz <EMAIL REMOVED> schreef in berichtnieuws
EMAIL REMOVED...

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



   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: While Not EOF(fn) ... Wend
Old
  (#6)
Larry Serflaten
Guest
 
Posts: n/a
Default Re: While Not EOF(fn) ... Wend - 06-04-2007, 08:54 AM

"Baz" <EMAIL REMOVED> 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! =-----
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: While Not EOF(fn) ... Wend
Old
  (#7)
J French
Guest
 
Posts: n/a
Default Re: While Not EOF(fn) ... Wend - 06-04-2007, 08:54 AM

On Thu, 04 Dec 2003 10:46:36 +0000, Baz <EMAIL REMOVED>
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 Cl*** that will give you some ideas

HTH

VERSION 1.0 CL***
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 Cl***_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 Cl***_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


   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Contact Us - Forum Care Forums - Archive - Top