none
EOF RRS feed

  • Question

  • I am currently running some code where I import data from a text file to a sheet through a line input statement. I loop through the text file by do until EOF(1). The problem I have is that I get EOF(1)=true before the entire file is read. To me it seems that EOF(1)=true is returned randomly. Furthermore, when I step through the code using the debugger, the whole procedure works fine, and EOF(1)=true is returned at the end of the file.

    I have also tried using the fileSystemObject and the AtEndOfStream command, but it does not make a difference.

    Does anybody have an idea how to solve this?

    Regards, Ola
    Thursday, August 28, 2008 11:06 AM

Answers

  • OK, so I found the problem my self.
    In another part of the program I am using a shellexecute command. If this command is commented out, the problem disappears. (out of pure laziness, I didn't write a function to wait until the shellexecute program was finnished). Sorry I waste your time.

    Thanks for all your help,

    Sincerely,

    Ola
    Friday, August 29, 2008 1:58 PM

All replies

  • Try this code. Please check whether you are using FreeFile() to get the file handle.

    Dim filenum  As Integer, contents As String, filename As String  
     
    filename = "C:\a.txt" 
    filenum = FreeFile 
     
    Open filename For Input As #filenum  
     
        Do Until EOF(filenum)  
            Line Input #filenum, contents  
              
            Dim num As String  
            num = contents
              
            Debug.Print num  
        Loop  
     
    Close #filenum 
    Thursday, August 28, 2008 12:05 PM
  •  I tried it, and it didn't work. Thanks anyway.

    Any other suggestions?
    Thursday, August 28, 2008 12:25 PM
  • Hi ola123,

    Try a StreamReader instead.

    Try this code with one button and one RichTextBox on a FORM please.

    You need to process each line of text read in by the StreamReader where highlighted in yellow below.>>




    Public Class Form1

      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

       'Clear the RichTextBox.>>
        RichTextBox1.Clear()
        'Create a NEW OpenFileDialog.>>
        Dim ofd As New OpenFileDialog
        'Filter for TEXT files only.>>
        ofd.Filter = "Text files only|*.txt"
        'Start off with the MyDocuments folder.>>
        ofd.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
        'Required for DialogResult.>>
        Dim result As DialogResult
        'Show the Dialog and get the result of the user action.>>
        result = ofd.ShowDialog
        'If the user clicked on OK then.>>
        If result = Windows.Forms.DialogResult.OK Then
          'Pass the file selected to a NEW StreamReader.>>
          Dim sr As New System.IO.StreamReader(ofd.FileName)
          Dim fileTextLine As String

          'While NOT at the end of the file.>>
          While sr.EndOfStream = False
            fileTextLine = sr.ReadLine

            'Process the line of text here.>>
     RichTextBox1.AppendText(fileTextLine)


          End While

          'Close the file.>>
          sr.Close()
          'Dispose of the StreamReader.>>
          sr.Dispose()

        End If

      End Sub
    End Class




    Regards,

    John


    .--- --- .... -.
    Thursday, August 28, 2008 2:10 PM
  • Hi,
    I am using VBA in excel, and as I understand it, streamreader is not available in VBA.

    Sincerely, Ola
    Friday, August 29, 2008 7:59 AM
  • Is the file too big in size?

    Can you please post the code or provide more info?

    Friday, August 29, 2008 8:36 AM
  •  The file is about 22000KB.

    I post some code:

        Do Until  EOF(filenum) = True
            Line Input #filenum, strLine

            If InStr(strLine, strCondition) > 0 Then
                dblNomTrades = dblNomTrades + 1
                Do While lngPosnew > 0
                    rngOutput.Offset(lngRow, lngCol) = Mid(strLine, lngPosOld + 1, lngPosnew - lngPosOld - 1)
                    lngPosOld = lngPosnew
                    lngCol = lngCol + 1
                    lngPosnew = InStr(lngPosOld + 1, strLine, strSeparator)
                Loop
                rngOutput.Offset(lngRow, lngCol) = Mid(strLine, lngPosOld + 1)
                lngCol = 0
                lngPosnew = 1
                lngPosOld = 0
                lngRow = lngRow + 1
           End If
        Loop


    Sincerely,

    Ola
    Friday, August 29, 2008 10:15 AM
  •   Can you please try running the code after commenting out the if block? We will try to isolate the issue.


    Do Until EOF(filenum) = True  
        Line Input #filenum, strLine  
    Loop  
     
    Friday, August 29, 2008 12:20 PM
  • Hi,
    Tried it, didn't help.
    The weird thing is that it is completely random when EOF is returned.
    Friday, August 29, 2008 1:35 PM
  • OK, so I found the problem my self.
    In another part of the program I am using a shellexecute command. If this command is commented out, the problem disappears. (out of pure laziness, I didn't write a function to wait until the shellexecute program was finnished). Sorry I waste your time.

    Thanks for all your help,

    Sincerely,

    Ola
    Friday, August 29, 2008 1:58 PM
  • ola123 said:

    Hi,
    I am using VBA in excel, and as I understand it, streamreader is not available in VBA.

    Sincerely, Ola


    Hi again ola123,

    Please read this post as these forums do not support VBA questions or questions for VbScript or questions for 
    Visual Basic versions 1 through to Visual Basic version 6.0.>>

    http://forums.msdn.microsoft.com/en-US/vblanguage/thread/28d32f74-9fcc-46cd-b9d2-28d70592d570




    Regards,

    John


    .--- --- .... -.
    Sunday, August 31, 2008 6:29 AM