【问题标题】:I cant write data to a binary file in a loop我无法循环将数据写入二进制文件
【发布时间】:2017-02-27 05:41:31
【问题描述】:

我的所有数据(名称/卷号/类)都是固定长度(14 字节),我希望程序继续从数组中获取一个随机名称,然后生成一个随机卷号并继续将其写入文件直到我按“A”。

在我运行完程序后,前 14 个字节总是空的,然后我的数据在接下来的 14 个字节中。不多不少。不知道为什么只写了一条数据记录。

这是我的代码:

    Dim randomvalue As Integer

    'Making this array so that each data entry has 14 bytes to it

    Dim array1(91) As Integer

    array1(1) = 14
    For x = 2 To 91
        array1(x) = array1(x) + 14
    Next

    Dim n1, n2, n3, n4 As Integer

    'Array with names of fixed lenght, I want to take a name from only these 9:

    Dim array2(9) As String
    array2(1) = "aleen"
    array2(2) = "talha"
    array2(3) = "waddi"
    array2(4) = "hasna"
    array2(5) = "hassa"
    array2(6) = "zainn"
    array2(7) = "faqeh"
    array2(8) = "furru"
    array2(9) = "ibrah"

  'There is a structure above submain with studentname/rollnumber/class all declared as string 

    Dim newstudent As student
    Dim studentstream As New FileStream("C:\Users\Students\Desktop\A2Filing.dat", FileMode.OpenOrCreate)
    Dim bw As New BinaryWriter(studentstream)

    While Console.ReadLine <> "A"
        Console.WriteLine("Press any key other than 'A' if you want to make another entry into the file")


        'CInt(Math.Floor((upperlimit - lowerlimit + 1) * Rnd())) + lowerlimit
        'Randomize a number b/w 1-9 to get a studentname from the array

        randomvalue = CInt(Math.Floor((9 - 1 + 1) * Rnd())) + 1
        newstudent.studentname = array2(randomvalue)

        n1 = Rnd() + (Rnd() * 50)
        n2 = Rnd() * 10
        n3 = Rnd() * 255
        n4 = Rnd() * Rnd()
        newstudent.rollnumber = Convert.ToString(Left(n1, 1) + Left(n2, 1) + Left(n3, 1) + Left(n4, 1))

        newstudent.studentclass = "A2"

        'Randomize a number between 91 and 1 to place the data in

        Dim randomvalue2 As Integer
        randomvalue2 = CInt(Math.Floor((91 - 1 + 1) * Rnd())) + 1
        studentstream.Position = array1(randomvalue2)


        bw.Write(newstudent.studentname)
        bw.Write(newstudent.rollnumber)
        bw.Write(newstudent.studentclass)
    End While

【问题讨论】:

    标签: vb.net binaryfiles


    【解决方案1】:

    这里有一些注释:

    • 您初始化时的array1 项目将全部为14,您必须将其更改为跟随,但正如我稍后所说,不需要定义这样的数组!

      For x = 2 To 91
        array1(x) = array1(x - 1) + 14   'modified to array1(x - 1)
      Next
      
    • bw.Write 之前,你有这个:studentstream.Position = array1(randomvalue2),它将覆盖文件位置! array1 中的所有项目也等于 14。这就是为什么你总是得到一个小文件大小(不多不少)。您不需要以这种方式设置位置。 只需从您的代码中删除该行即可逐个附加数据。

    • 根据文档,如果您使用BinaryWriter 编写字符串,它将以字符串长度为前缀! see this question

      Dim bw As New BinaryWriter(studentstream, System.Text.Encoding.ASCII)
      
      bw.Write("A")  'writes: 1 A  => where 1 is length of string "A"
      bw.Write("BC") 'writes: 2 B  => where 2 is length of string "BC" 
      
      'note that for Unicode Encoding, it will be 2 bytes per char
      Dim bw As New BinaryWriter(studentstream, System.Text.Encoding.Unicode)
      bw.Write("A")  '2 A 0        where 2 is length of unicde string [A 0]
      bw.Write("BC") '4 B 0 C 0    where 4 is length of unicde string [B 0 C 0]
      
    • 如果要写不带长度前缀的字符串,可以使用System.Text.Encoding.ASCII.GetBytes(str)将其转为字节数组:

      bw.Write(System.Text.Encoding.ASCII.GetBytes(newstudent.studentname))
      
    • 另一个选项也是使用StreamWriter 而不是BinaryWriter,因为您似乎不需要它。 see this link

    • 请注意,最好使用 Using 块打开文件。否则,完成后不要忘记调用Close 方法(studentstream.Close())。如果您不这样做,您可能会丢失最后一块或全部数据!

      Using studentstream As New FileStream("C:\...\Desktop\A2Filing.dat", FileMode.Create)
      
    • 如果文件已存在,请使用FileMode.Create 覆盖该文件

    【讨论】:

    • 谢谢!它起作用了,我决定保留这个 studentstream.Position = array1(randomvalue2) 因为我不想将数据附加到文件中,我想把它放在一个随机的地方(也做了一个溢出的东西:一个不断添加的 while 循环 + 1 到 studentstream.position 直到遇到空白)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-15
    • 1970-01-01
    • 2014-05-20
    • 2017-05-08
    • 1970-01-01
    相关资源
    最近更新 更多