我的所有数据(名称/卷号/类)都是固定长度(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
谢谢!它的工作,我决定保留这个studentstream.Position = array1(randomvalue2),因为我不想追加数据到文件,我想把它放在一个随机的地方(做一个溢出thingy:一个while循环,不断添加+ 1到studentstream.position,直到它遇到一个空的空间) –