【问题标题】:Need help writing data to a txt file and getting it to save to this file需要帮助将数据写入 txt 文件并将其保存到此文件
【发布时间】:2014-08-04 01:11:15
【问题描述】:

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

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

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

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 的新手,希望能提供任何帮助。

【问题讨论】:

    标签: vb.net vb.net-2010


    【解决方案1】:

    乍一看,您似乎没有关闭文件或刷新缓冲区。完成写入后,您至少需要执行 fileYouCreated.close() 。查看http://www.tutorialspoint.com/vb.net/vb.net_file_handling.htmhttp://support.microsoft.com/kb/304427 了解有关读取/写入文件的更多信息。

    【讨论】:

    • 谢谢。我能够将数据保存到文件中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-22
    • 2018-04-12
    • 1970-01-01
    相关资源
    最近更新 更多