需要帮助将数据写入txt文件并将其保存到此文件

时间:2014-08-03 17:06:56

标签: vb.net vb.net-2010

所以我有一个项目,我的任务是创建一个应用程序,它使用一个结构存储有关客户帐户的以下数据:姓氏,名字,客户编号,地址,城市,州,邮政编码,电话号码,帐户余额和上次付款日期。 应用程序应允许用户将客户帐户记录保存到文件中,按姓氏或客户编号搜索文件以查找客户,并打印列出文件中所有客户记录的报告。

输入验证:输入新记录时,请确保用户输入所有字段的数据。不接受帐户余额的负数。

这是我到目前为止的代码,但我遇到了一些问题: 当我输入数据时,会创建一个文件,但它是“空白”,我无法弄清楚为什么信息不会保存到文件中。

Imports System.IO

Imports System.IO.FileStream

Public Class frmCustAcct

    Dim searchFile As StreamReader

    'Declare Structure
    Structure CustomerAccounts
        Dim LastName As String           ' Last name of customer
        Dim FirstName As String         ' First name of customer
        Dim CustomerNumber As String    ' Customer number
        Dim Address As String           ' Address of customer
        Dim City As String              ' City of customer
        Dim State As String             ' State of customer
        Dim ZipCode As Integer          ' Zip code of customer
        Dim TelephoneNumber As Int64    ' Phone number of customer
        Dim AccountBalance As Double    ' Customer account balance
        Dim DateofLastPayment As String ' Date of customer's last payment
    End Structure

    Private Sub SaveToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles SaveToolStripMenuItem.Click

        ' Assign Structure Variables
        Dim inputFile As StreamWriter  ' Object variable
        Dim CustomerRecord As CustomerAccounts ' Structure variable

        'Opening Files
        inputFile = File.CreateText("E:\Rio Salado\2014\CIS159 - 11480\HOMEWORK\Chapter 12\Records.txt")

        ' Assign Structure Variables
        CustomerRecord.LastName = txtLast.Text
        CustomerRecord.FirstName = txtFirst.Text
        CustomerRecord.CustomerNumber = txtCustNum.Text()
        CustomerRecord.Address = txtAdd.Text
        CustomerRecord.City = txtCity.Text
        CustomerRecord.State = txtState.Text
        CustomerRecord.ZipCode = CInt(txtZip.Text)
        CustomerRecord.TelephoneNumber = CInt(txtTeleNum.Text)
        CustomerRecord.AccountBalance = CDbl(txtAcctBal.Text)
        While CustomerRecord.AccountBalance < 0
            CustomerRecord.AccountBalance = CDbl(InputBox("Enter non-Negative Balance"))
        End While
        CustomerRecord.DateofLastPayment = CStr(CDate(txtLastPay.Text))

        'Write the data to the file. 
        inputFile.WriteLine(CustomerRecord.LastName)
        inputFile.WriteLine(CustomerRecord.FirstName)
        inputFile.WriteLine(CustomerRecord.CustomerNumber)
        inputFile.WriteLine(CustomerRecord.Address)
        inputFile.WriteLine(CustomerRecord.City)
        inputFile.WriteLine(CustomerRecord.State)
        inputFile.WriteLine(CustomerRecord.ZipCode)
        inputFile.WriteLine(CustomerRecord.TelephoneNumber)
        inputFile.WriteLine(CustomerRecord.AccountBalance)
        inputFile.WriteLine(CustomerRecord.DateofLastPayment)
        ClearFields()

    End Sub

    Private Sub SearchToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles SearchToolStripMenuItem.Click

        'Open File to Search Existing
        searchFile = File.OpenText("E:\Rio Salado\2014\CIS159 - 11480\HOMEWORK\Chapter 12\Records.txt")
        Dim LastName As String
        Dim Flag As Integer
        Flag = 0
        'Retrieve the Record Name
        LastName = InputBox("Enter Last Name to Search")
        Dim CSearchRecord As CustomerAccounts
        Try
            While Not searchFile.EndOfStream
                CSearchRecord.LastName = searchFile.ReadLine()
                CSearchRecord.FirstName = searchFile.ReadLine()
                CSearchRecord.CustomerNumber = searchFile.ReadLine()
                CSearchRecord.Address = searchFile.ReadLine()
                CSearchRecord.City = searchFile.ReadLine()
                CSearchRecord.State = searchFile.ReadLine()
                CSearchRecord.ZipCode = CInt(searchFile.ReadLine())
                CSearchRecord.TelephoneNumber = CLng(searchFile.ReadLine())
                CSearchRecord.AccountBalance = CDbl(searchFile.ReadLine())
                CSearchRecord.DateofLastPayment = searchFile.ReadLine()
                'Compare Current Record With Searched
                If CSearchRecord.LastName.Equals(LastName) Then
                    Flag = 1
                    Exit While
                End If
            End While
            'If Record Found Display Appropriate Fields
            If Flag.Equals(1) Then
                txtLast.Text = CSearchRecord.LastName.ToString()
                txtFirst.Text = CSearchRecord.FirstName.ToString()
                txtCustNum.Text = CSearchRecord.CustomerNumber.ToString()
                txtAdd.Text = CSearchRecord.Address.ToString()
                txtCity.Text = CSearchRecord.City.ToString()
                txtState.Text = CSearchRecord.State.ToString()
                txtZip.Text = CSearchRecord.ZipCode.ToString()
                txtTeleNum.Text = CSearchRecord.TelephoneNumber.ToString()
                txtAcctBal.Text = CSearchRecord.AccountBalance.ToString()
                txtLastPay.Text = CSearchRecord.DateofLastPayment.ToString()
            Else
                'if not tell user no record Exists
                MessageBox.Show("No Record Found")
                ClearFields()
            End If
        Catch ex As Exception
        End Try

    End Sub

    Private Sub ReportToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ReportToolStripMenuItem.Click

        Dim report As String
        report = "Report of Customer Accounts" + vbNewLine
        ' Open a File in Read Mode
        searchFile = File.OpenText("E:\Rio Salado\2014\CIS159 - 11480\HOMEWORK\Chapter 12\Records.txt")
        Try
            'Reading the file until complete
            While Not searchFile.EndOfStream
                report += searchFile.ReadLine() + ""
                report += searchFile.ReadLine() + ""
                report += searchFile.ReadLine() + ""
                report += searchFile.ReadLine() + ""
                report += vbNewLine
            End While
        Catch ex As Exception
        End Try
        'Display Report
        MessageBox.Show(report)
    End Sub
    Private Sub ClearFields()
        txtAcctBal.clear()
        txtadd.clear()
        txtCity.Clear()
        txtFirst.Clear()
        txtLast.Clear()
        txtCustNum.clear()
        txtLastPay.Clear()
        txtState.Clear()
        txtTeleNum.clear()
        txtZip.Clear()
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click

        'Close the Form
        Me.Close()
    End Sub
End Class

我是Visual Basic的新手,非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

乍一看,您似乎没有关闭文件或刷新缓冲区。当你完成写入时,你至少需要做fileYouCreated.close()。有关读取/写入文件的其他信息,请查看http://www.tutorialspoint.com/vb.net/vb.net_file_handling.htmhttp://support.microsoft.com/kb/304427