文件来自另一个项目,但不会读取或加载到数组中

时间:2021-12-12 11:15:38

I'm trying to use a file from one project in a new project. The code has no errors in debugging, but skips to my programmed error "Problem reading data from file." Any attempt to run the program anyway results in a calculation of 0 - because the arrays don't seem to be populating.

我正在尝试在一个新项目中使用一个项目中的文件。这段代码在调试过程中没有出现错误,但会跳转到我的程序错误“从文件中读取数据的问题”。任何试图运行该程序的尝试都会导致计算结果为0——因为数组似乎没有填充。

Thanks for the help.

谢谢你的帮助。

'opens the text file, reads it, stores the value in two arrays
    'closes the file

    Dim srFileName As String   'Name the file to be opened
    Dim intNumItems As Integer  'number of records on file
    Dim i As Integer            'loop counter for the array 
    Dim intLastBlank As Integer
    Dim strInput As String

    srFileName = "C:\StudentMarks.txt"



    If File.Exists(srFileName) Then
        Dim srReader As New StreamReader(srFileName)

        Try

            intNumItems = srReader.ReadLine()
            strInput = srReader.ReadLine()

            Do While srReader.Peek >= 0

            Loop
            strInput = srReader.ReadLine()


            While Not srReader.EndOfStream

                'each loop iteration, reads 2 lines and places in proper array
                For i = 0 To intNumItems - 1

                    'find location of tab between student name and mark
                    intLastBlank = strInput.IndexOf(vbTab)
                    'take characters before tab and place in name array
                    arrName(i) = strInput.Substring(intLastBlank + 1)
                    'take characters after tab, convert to double, and
                    'place values in mark array
                    arrMark(i) = Double.Parse(strInput.Substring(intLastBlank + 1))

                Next
            End While

        Catch ex As Exception
            MessageBox.Show("Problem reading data from file")
        End Try

1 个解决方案

#1


0  

You really should make your MessageBox display the actual error message too. It will help you troubleshooting the problem.

您确实应该让MessageBox显示实际的错误消息。它将帮助您解决问题。

Example:

例子:

MessageBox.Show("Problem reading data from file:" & Environment.NewLine & ex.Message)

Anyhow, as for your problem you have created an infinite loop here:

不管怎样,对于你的问题,你在这里创造了一个无限循环:

Do While srReader.Peek >= 0
    'You have no code here, so you've created yourself an infinite loop.
Loop
strInput = srReader.ReadLine()

You probably misplaced the Loop keyword. I'm guessing it's supposed to be further down the code?

你可能把Loop关键字放错地方了。我猜它应该在代码的后面?


EDIT:

编辑:

I also don't understand the point of using both:

我也不明白两者都用的意义:

Do While srReader.Peek >= 0

...and:

,:

While Not srReader.EndOfStream

You only need to use one of the statements, as both of them checks wether there is more for the StreamReader to read.

您只需要使用其中一个语句,因为这两个语句都要检查StreamReader是否还有其他内容需要阅读。

Also make sure you put the .ReadLine() methods within the loop. :)

还要确保将. readline()方法放在循环中。:)

#1


0  

You really should make your MessageBox display the actual error message too. It will help you troubleshooting the problem.

您确实应该让MessageBox显示实际的错误消息。它将帮助您解决问题。

Example:

例子:

MessageBox.Show("Problem reading data from file:" & Environment.NewLine & ex.Message)

Anyhow, as for your problem you have created an infinite loop here:

不管怎样,对于你的问题,你在这里创造了一个无限循环:

Do While srReader.Peek >= 0
    'You have no code here, so you've created yourself an infinite loop.
Loop
strInput = srReader.ReadLine()

You probably misplaced the Loop keyword. I'm guessing it's supposed to be further down the code?

你可能把Loop关键字放错地方了。我猜它应该在代码的后面?


EDIT:

编辑:

I also don't understand the point of using both:

我也不明白两者都用的意义:

Do While srReader.Peek >= 0

...and:

,:

While Not srReader.EndOfStream

You only need to use one of the statements, as both of them checks wether there is more for the StreamReader to read.

您只需要使用其中一个语句,因为这两个语句都要检查StreamReader是否还有其他内容需要阅读。

Also make sure you put the .ReadLine() methods within the loop. :)

还要确保将. readline()方法放在循环中。:)