保存二进制文件

时间:2016-05-17 15:44:08

标签: vb.net

嘿伙计们,我对计算机科学比较陌生,我想知道为什么我的代码不起作用。我的老师告诉我设计一张名片并将其保存到二进制文件中,但是当我保存时,我检查文件,那里什么都没有。当我加载文件时没有输出任何内容,所以我想知道是否有人可以帮忙

代码:

Imports System.IO

Module Module1

    Dim firstname As String
    Dim secondname As String
    Dim address As String
    Dim postcode As String
    Dim telephone As String
    Dim email As String
    Structure Details
        Dim firstname As String
        Dim secondname As String
        Dim address As String
        Dim postcode As String
        Dim telephone As String
        Dim email As String
    End Structure
    Sub Main()
        Console.WriteLine("Do you want to  (l)oad, or (c)hose?")
        Dim decision As String = Console.ReadLine.ToUpper
        If decision = "L" Then
            load()
            Main()
        End If
        If decision = "C" Then
            chose()
            Console.WriteLine("Saving")
            save()
            Main()
        End If
        Console.Read()
    End Sub
    Private Sub load()
        Console.WriteLine("File name?")
        Dim filename As String = Console.ReadLine + ".txt"
        If (File.Exists(filename)) Then
            Dim det As Details = New Details()
            FileOpen(1, filename, OpenMode.Binary)
            FileGet(1, det)
            FileClose()
            firstname = det.firstname
            secondname = det.secondname
            address = det.address
            postcode = det.postcode
            telephone = det.telephone
            email = det.email
            Console.WriteLine("Firstname =" & firstname)
            Console.WriteLine("secondname =" & secondname)
            Console.WriteLine("address =" & address)
            Console.WriteLine("postcode =" & postcode)
            Console.WriteLine("telephone =" & telephone)
            Console.WriteLine("email =" & email)
        End If
    End Sub
    Private Sub save()
        Console.WriteLine("Enter a name for your file")
        Dim filename As String = Console.ReadLine() + ".txt"
        Dim det As Details = New Details()
        det.firstname = firstname
        det.secondname = secondname
        det.address = address
        det.postcode = postcode
        det.telephone = telephone
        det.email = email

        FileOpen(1, filename, OpenMode.Binary)
        FilePut(1, det)
        FileClose()
    End Sub
    Private Sub chose()
        Console.WriteLine()
        Console.WriteLine("Enter your first name")
        Dim firstname As String = Console.ReadLine
        Console.WriteLine("Enter your second name")
        Dim secondname As String = Console.ReadLine
        Console.WriteLine("Enter your adress ")
        Dim address As String = Console.ReadLine
        Console.WriteLine("Enter your postcode")
        Dim postcode As String = Console.ReadLine
        Console.WriteLine("Enter your telephone")
        Dim telephone As String = Console.ReadLine
        Console.WriteLine("Enter your email")
        Dim email As String = Console.ReadLine
    End Sub
End Module

1 个答案:

答案 0 :(得分:1)

您已经全局声明了firstname,secondname等变量,因此不要在chose Sub中再次标注它们,否则它们将是不同的局部变量。

应该是:

Private Sub chose()
        Console.WriteLine()
        Console.WriteLine("Enter your first name")
        firstname = Console.ReadLine
        Console.WriteLine("Enter your second name")
        secondname = Console.ReadLine
        Console.WriteLine("Enter your adress ")
        address = Console.ReadLine
        Console.WriteLine("Enter your postcode")
        postcode = Console.ReadLine
        Console.WriteLine("Enter your telephone")
        telephone = Console.ReadLine
        Console.WriteLine("Enter your email")
        email = Console.ReadLine
    End Sub