2017-02-27 3 views
0

Toutes mes données (nom/numéro/classe) sont d'une longueur fixe (14 octets), je veux que le programme continue à prendre un nom aléatoire du tableau, puis faites un numéro de rouleau aléatoire et continuez à l'écrire dans un fichier jusqu'à ce que j'appuie sur "A". Après avoir fini d'exécuter le programme, les 14 premiers octets sont toujours vides et mes données sont dans les 14 octets suivants. Ni plus ni moins. Je ne sais pas pourquoi il a seulement écrit un enregistrement de données.Je ne peux pas écrire des données dans un fichier binaire dans une boucle

Ceci est mon code:

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 

Répondre

1

Il y a quelques notes:

  • les array1 articles que vous init tous être 14, vous devez changer à suivre, mais comme Je dirai plus tard qu'il n'est pas nécessaire de définir un tel tableau!

    For x = 2 To 91 
        array1(x) = array1(x - 1) + 14 'modified to array1(x - 1) 
    Next 
    
  • avant bw.Write, vous avez ceci: studentstream.Position = array1(randomvalue2) qui remplacera la position de fichier! tous les articles dans array1 sont égaux à 14. C'est pourquoi vous obtenez toujours une petite taille de fichier (rien de plus rien de moins). Vous n'avez pas besoin de régler la position de cette façon. Supprimez simplement cette ligne de votre code pour ajouter des données une après l'autre. Selon les documentations, si vous utilisez un BinaryWriter pour écrire des chaînes, il sera préfixé par string-length! 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] 
    
  • Si vous voulez écrire des chaînes sans préfixe de longueur, vous pouvez utiliser System.Text.Encoding.ASCII.GetBytes(str) pour le convertir en tableau d'octets:

    bw.Write(System.Text.Encoding.ASCII.GetBytes(newstudent.studentname)) 
    
  • Une autre option sera également utiliser StreamWriter au lieu de BinaryWriter comme On dirait que tu n'en as pas besoin. see this link

  • Notez qu'il est préférable d'ouvrir les fichiers avec Using bloquer. sinon, n'oubliez pas d'appeler la méthode Close (studentstream.Close()) lorsque vous avez terminé. Si vous ne le faites pas, vous risquez de perdre la dernière partie ou l'ensemble de vos données!

    Using studentstream As New FileStream("C:\...\Desktop\A2Filing.dat", FileMode.Create) 
    
  • utilisation FileMode.Create pour remplacer le fichier s'il existe déjà

+0

merci! Cela a fonctionné, j'ai décidé de garder cette studentstream.Position = array1 (randomvalue2) parce que je ne voulais pas ajouter de données au fichier, je veux le mettre dans un endroit aléatoire (fait un overy chose aussi: une boucle while qui ajoute + 1 à la classe studentstream.position jusqu'à ce qu'elle rencontre un espace vide) –