Click here to Skip to main content
16,018,805 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I need some help regarding reading data from a file in vb6,my problem is I have a file containing 2000 lines but i want to read only 183 lines but in my code I'm reading the entire file.

pls solve my problem

Below I have provided only one part of my code related to file input.

Thank You

What I have tried:

f = FreeFile
Open FileName For Input As #f

Do While Not EOF(1)
Line Input #f, FileLine


................code here....................


loop
close #f
Posted
Updated 10-May-18 1:18am
v2

Here is a more complete VB.Net solution based on line numbers if anyone happens to have the same problem sometime later ...

VB
' Read each line in a text file and print its contents line by line
Module Module1

    Sub Main()
        Dim lineNumber As Integer
        Dim lineOfText As String
        Dim filestream = New System.IO.FileStream("TextFile.txt", System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite)
        Dim file = New System.IO.StreamReader(filestream, System.Text.Encoding.UTF8, True, 128)

        lineNumber = 0
        lineOfText = file.ReadLine()
        While (lineOfText IsNot Nothing)

            Console.WriteLine("{0} '{1}'", lineNumber, lineOfText)
            lineNumber = lineNumber + 1
            lineOfText = file.ReadLine()

        End While

        Console.ReadKey()
    End Sub

End Module
 
Share this answer
 
v2
A definitive answer is not possible because your not telling anything about the file structure or what you actually want to read. Consider the following program:

Did you want to:
1> read a certain line with a certain content e.g. Lines that start with 'ReadThis' or
2> did you want to read a certain set of lines e.g. Line 1000-1183?

I'll modify the code depending on what you want - please state more details about the file content and what you need to read ...

VB
Imports System
Imports System.IO

Public Class Program

    Public Shared Sub Main()
        Try
            Using sr As StreamReader = New StreamReader("TestFile.txt")
                Dim line As String = sr.ReadToEnd()
                Console.WriteLine(line)
            End Using
        Catch e As Exception
            Console.WriteLine("The file could not be read:")
            Console.WriteLine(e.Message)
        End Try
    End Sub
End Class
 
Share this answer
 
Comments
Member 13396059 10-May-18 7:01am    
Solved it Thank u
I have used
if count >182 then
exit do
end if
CHill60 10-May-18 7:27am    
OP is using VB6
Introduce a line counter and stop when you get to 183
f = FreeFile
Open FileName For Input As #f
Dim lc As Integer
lc = 1
Do While Not EOF(1) and lc < 184
    Line Input #f, FileLine
    ................code here....................

    lc = lc + 1
loop
close #f 
 
Share this answer
 
Comments
Member 13396059 10-May-18 7:01am    
Solved it Thank u
I have used
if count >182 then
exit do
end if
CHill60 10-May-18 7:26am    
That will also work but IMhO it is better practice to make the loop conditions obvious "up front" rather than using the equivalent of a goto. However, as long as it works.
By the way, if possible, you should really not be using VB6 for new code. It is no longer supported (hasn't been for decades) and VB.NET is free to use.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900