关于从文件中读取数据的VB6编程 [英] VB6 programming about reading data from a file

查看:82
本文介绍了关于从文件中读取数据的VB6编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我需要一些关于从vb6中的文件读取数据的帮助,我的问题是我有一个包含2000行的文件,但我想只读183行,但在我的代码中,我正在阅读整个文件。



请解决我的问题



下面我只提供了与文件输入相关的代码的一部分。



谢谢



< b>我尝试了什么:



f = FreeFile

打开FileName输入为#f



不做EOF(1)

线路输入#f,FileLine





................代码在这里....................





循环

关闭#f

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

推荐答案

介绍一个行计数器当你到达时停止183

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 


明确的答案是不可能,因为你没有告诉任何关于文件结构或你真正想要阅读的内容。考虑以下计划:



您想要:

1>读取具有特定内容的某一行,例如以'ReadThis'开头的行或

2>你想阅读某些行吗? 1000-1183行?



我会根据你的需要修改代码 - 请说明有关文件内容的更多细节以及你需要阅读的内容......



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 ...

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


这是一个更完整的 VB.Net 解决方案,基于行号,如果有人碰巧在以后的某个时间遇到同样的问题...



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

' 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


这篇关于关于从文件中读取数据的VB6编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆