Dim file As System.IO.StreamWriter Dim nom As String Dim prenom As String file = My.Computer.FileSystem.OpenTextFileWriter("e:\test.txt", True) nom = InputBox("Votre Nom") file.Write(nom & " ") Prenom = InputBox("Votre Prénom") file.Write(prenom & " ") file.Close()
Imports System.IO Module Module3 Dim file As System.IO.StreamWriter Dim listitems() As String Dim coordononee() As String Dim nom As String Dim prenom As String Dim score As String Dim found As Boolean Dim index As Integer Sub Main() nom = InputBox("Votre Nom") prenom = InputBox("Votre Prénom") found = False If checkfile() Then writetofile() End If End Sub Private Function checkfile() As Boolean Try listitems = System.IO.File.ReadAllLines("e:\test.txt") For iter = 0 To listitems.Length - 1 coordononee = listitems(iter).Split(" ") If coordononee(0).ToString = "" Then found = False Exit For Else If coordononee(0) nom And coordononee(1) prenom Then If coordononee.Length > 3 Then Console.WriteLine("your last scrore is " & coordononee(2)) End If index = iter found = True End If End If Next Catch ex As Exception Console.Write("erreur fichier introuvable") Return False Exit Function End Try Return True End Function Private Sub writetofile() If Not found Then Try File = My.Computer.FileSystem.OpenTextFileWriter("e:\test.txt", True) File.Write(nom & " ") File.WriteLine(prenom) File.Close() Catch ex As Exception Console.Write("erreur fichier introuvable") Exit Sub End Try End If If found Then Try File = My.Computer.FileSystem.OpenTextFileWriter("e:\test.txt", False) score = InputBox("Votre score") listitems(index) = nom & " " & prenom & " " & score Dim chaine As String For Each chaine In listitems File.WriteLine(chaine) Next File.Close() Catch ex As Exception Console.Write("erreur fichier introuvable") Exit Sub End Try End If End Sub End Module
Imports System.IO Module Module3 Dim file As System.IO.StreamWriter Dim listitems() As String Dim nom As String Dim prenom As String Dim score As String Dim found As Boolean Dim index As Integer Dim delimiter As Char 'vaut mieux écrire les coordonnées du candidat sur une meme ligne dans le fichier text ' le candidat peut avoir plusieurs prénom Jean claude etc Sub Main() delimiter = " " nom = InputBox("Votre Nom") prenom = InputBox("Votre Prénom") found = False If checkfile() Then writetofile() End If End Sub Private Function checkfile() As Boolean 'verifie si le fichier existe Try 'copy tout le fichier dans un array of string listitems = System.IO.File.ReadAllLines("e:\test.txt") For iter = 0 To listitems.Length - 1 ' cherche si le candidat existe si oui found se met à true If listitems(iter).IndexOf(nom) >= 0 And listitems(iter).IndexOf(prenom) >= 0 Then index = iter found = True End If Next Catch ex As Exception Console.Write("erreur fichier introuvable") Return False Exit Function End Try Return True End Function Private Sub writetofile() Dim coordononee() As String ' si ç'est un nouveau candidat If Not found Then Try 'ouvre le fichier pour écrire mode append true file = My.Computer.FileSystem.OpenTextFileWriter("e:\test.txt", True) file.Write(nom & delimiter) file.WriteLine(prenom) file.Close() Catch ex As Exception Console.Write("erreur fichier introuvable") Exit Sub End Try End If ' si le candidat existe If found Then Try 'ouvre le fichier pour écrire mode append false c.a.d on remplace 'le fichier existant file = My.Computer.FileSystem.OpenTextFileWriter("e:\test.txt", False) ' copie les données du candidat dans un array of string coordononee = listitems(index).Split(delimiter) Try 'verifie si le candidat a déja un scrore Double.Parse(coordononee(coordononee.Length - 1)) Console.WriteLine("votre dernier score " & coordononee(coordononee.Length - 1)) Catch ex As Exception End Try score = InputBox("Votre score") listitems(index) = nom & delimiter & prenom & delimiter & score 'on recopie tout le listitems dans le fchier Dim chaine As String For Each chaine In listitems file.WriteLine(chaine) Next file.Close() Catch ex As Exception Console.Write("erreur fichier introuvable") Exit Sub End Try End If End Sub End Module