IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

VB.NET Discussion :

liste chainee, fichier txt, VB


Sujet :

VB.NET

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Nouveau membre du Club
    Inscrit en
    Juillet 2010
    Messages
    5
    Détails du profil
    Informations forums :
    Inscription : Juillet 2010
    Messages : 5
    Par défaut liste chainee, fichier txt, VB
    bonjour tout le monde
    je viens de debuter la programmation en VB
    on ma donneé un projet qui conssite a jeré un bibiliotheque
    je compte mettre la liste des livres disponible dans un fichier txt comme suit

    TYPE_DU_LIVRE NOM_DE_LAUTEUR NOM_DU_LIVRE

    puis charger cette base de donnee dans un liste chainee contenant cette structure

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    'Declaration de la Structure de la Fiche_Du_Livre
        Structure Fiche_Du_Livre
            Dim Genre As String
            Dim Auteur As String
            Dim Titre As String
            Dim ID As Integer
    	Dim Disponibilite As Integer
        End Structure
    je voudrais aussi faire des recherche/ajout/modification/suppression dans cette chaine

    tout ce que je viens denumerer jarrive a les faire en C mais le projet doit etre en VB


    jaimerais avoir les fonctions permettant

    lire/ecrire dans un fichier TXT
    creation/chargement/moidification/affichage dune liste chainee

    donner moi des exemples a lappuis cela me ferait un tres grand plaisir


    plus precisement lequivalent de ces lignes de codes

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    ********************************************************
    fscanf(fichier, "%s %s %s %d %s\n", nouveau->Genre, nouveau->Auteur, nouveau->Titre, &nouveau->ID, nouveau->Disponibilite );//prendre les inforamtion se trouvant dans base_de_donnee.txt
    nouveau->suivant=0;                 //pointer suivant vers 0 ou NULL
                    if(debut==0)
                    {
                        debut=fin=nouveau;
                        nouveau->precedent=0;
                    }
                    else
                    {
                       nouveau->precedent=fin;
                       fin->suivant=nouveau;
                       fin=fin->suivant;
                       taille++;
                    }
    *********************************************************
     
     
    *********************************************************
    for ( i=0 ; i<qtyService ; i++)
        {
            printf("%d.- %s %d\n", i, courant->type, courant->tarif);
            courant=courant->suivant;
        }
    *********************************************************
    MERCI davance

  2. #2
    Expert éminent Avatar de Pol63
    Homme Profil pro
    .NET / SQL SERVER
    Inscrit en
    Avril 2007
    Messages
    14 200
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Puy de Dôme (Auvergne)

    Informations professionnelles :
    Activité : .NET / SQL SERVER

    Informations forums :
    Inscription : Avril 2007
    Messages : 14 200
    Par défaut
    c'est un exercice pour apprendre le vb.net ou un programme qui sera réellement utilisé

    dans le 2ème cas je te conseille de passer sur une base de données
    dans le 1er aussi d'ailleurs , enfin un fichier txt pour stocker des données c'est pas super ...
    Cours complets, tutos et autres FAQ ici : C# - VB.NET

  3. #3
    Membre extrêmement actif
    Inscrit en
    Avril 2008
    Messages
    2 573
    Détails du profil
    Informations personnelles :
    Âge : 65

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 573
    Par défaut exemple de liste chaine en vb.net,linkedlist
    bonjour
    si c'est pour apprendre le vb.net comme l'a dit tomlev,voici un code qui ressemble à ton code c.
    mais attention ton code c travaille au bas niveau et tu dois gerer toi meme ta liste.
    en vb.net une bonne partie de la gestion de liste chaine est faite par VB.Net.
    ton code traduit en vb.net pour lire le fichier texte ,inserer un element (after,before),visualiser le premier ,le dernier element .
    code:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
     
    Imports System
    Imports System.IO
    Imports System.Windows
    Imports System.Collections.Generic
    Public Class Form1
        Private objFiche As Fiche_Du_Livre
        Private lienLivre As LinkedList(Of Fiche_Du_Livre) = New LinkedList(Of Fiche_Du_Livre)
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
     
            cboBoxInsertBefore.DisplayMember = "Auteur"
            cboBoxInsertBefore.ValueMember = "ID"
            cboBoxInsertBefore.Enabled = False
            '
            cboBoxInsertAfter.DisplayMember = "Auteur"
            cboBoxInsertAfter.ValueMember = "ID"
            cboBoxInsertAfter.Enabled = False
        End Sub
        Private Sub btnOuvrirFichier_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOuvrirFichier.Click
            Dim fichierChemin As String = ""
            Me.OpenFileDialog1.Filter = "Fichier Texte(*.txt)|*.txt"
            If Me.OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                fichierChemin = Me.OpenFileDialog1.FileName
                If Len(fichierChemin) = 0 Then
                    MessageBox.Show("erreur fichier...")
                    Exit Sub
                Else
                    Call LitFichier(fichierChemin)
     
                End If
            End If
        End Sub
        Private Sub LitFichier(ByVal cheminfichier As String)
            Using monReader As New  _
            Microsoft.VisualBasic.FileIO.TextFieldParser(cheminfichier)
                monReader.TextFieldType = FileIO.FieldType.Delimited
     
                monReader.SetDelimiters(",")
                Dim ligneCourante As String()
                Dim afficheLigCourante As String = ""
                Dim numChamp As Integer = 1
                While Not monReader.EndOfData
                    Try
                        ligneCourante = monReader.ReadFields()
                        Dim champCourant As String = String.Empty
                        objFiche = New Fiche_Du_Livre
                        numChamp = 1
                        For Each champCourant In ligneCourante
                            Select Case numChamp
                                Case 1
                                    objFiche.Genre = CType(champCourant, String)
                                Case 2
                                    objFiche.Auteur = CType(champCourant, String)
                                Case 3
                                    objFiche.Titre = CType(champCourant, String)
                                Case 4
                                    objFiche.ID = Integer.Parse(champCourant)
                                Case 5
                                    objFiche.Disponibilite = Integer.Parse(champCourant)
                            End Select
                            'champ suivant
                            numChamp = numChamp + 1
                            afficheLigCourante = afficheLigCourante & champCourant
                            'MsgBox(champCourant)
                        Next
     
                        Call stockeFicheDuLIVRE(objFiche)
                        'ligne suivante
                        MessageBox.Show(afficheLigCourante)
                        afficheLigCourante = ""
                    Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
                        MsgBox("Ligne " & ex.Message & _
                        "non valide,sera saute....")
                    End Try
                End While
            End Using
        End Sub
        Private Sub stockeFicheDuLIVRE(ByVal objFiche As Fiche_Du_Livre)
            Dim noeud As LinkedListNode(Of Fiche_Du_Livre) = New LinkedListNode(Of Fiche_Du_Livre)(objFiche)
            lienLivre.AddFirst(noeud)
     
        End Sub
     
        Private Sub btnListeLie_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAffDGVListe.Click
            Call FillDataGridView()
        End Sub
     
        Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPremier.Click
            Call ClearTextBox()
            Me.txtGenre.Text = lienLivre.First.Value.Genre
            Me.txtAuteur.Text = lienLivre.First.Value.Auteur
            Me.txtTitre.Text = lienLivre.First.Value.Titre
            Me.txtID.Text = lienLivre.First.Value.ID.ToString
            Me.txtDisponibilte.Text = lienLivre.First.Value.Disponibilite.ToString
        End Sub
     
        Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDernier.Click
            Call ClearTextBox()
            Me.txtGenre.Text = lienLivre.Last.Value.Genre
            Me.txtAuteur.Text = lienLivre.Last.Value.Auteur
            Me.txtTitre.Text = lienLivre.Last.Value.Titre
            Me.txtID.Text = lienLivre.Last.Value.ID.ToString
            Me.txtDisponibilte.Text = lienLivre.Last.Value.Disponibilite.ToString
        End Sub
     
        Private Sub cboBoxInsertBefore_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboBoxInsertBefore.SelectedIndexChanged
            'Inserer nouvel ouvrage avant ouvrage -x-
            Dim nouvOuvrage As Fiche_Du_Livre = New Fiche_Du_Livre
     
            nouvOuvrage.Genre = txtGenre.Text
            nouvOuvrage.Auteur = txtAuteur.Text
            nouvOuvrage.Titre = txtTitre.Text
            If IsNumeric(txtID.Text) Then
                nouvOuvrage.ID = Integer.Parse(txtID.Text)
            Else
                MessageBox.Show("Err Texte Absent ou non Numerique...")
                txtID.Focus()
            End If
            If IsNumeric(txtDisponibilte.Text) Then
                nouvOuvrage.Disponibilite = Integer.Parse(txtDisponibilte.Text)
            Else
                MessageBox.Show("Err Texte Absent ou non Numerique...")
                txtDisponibilte.Focus()
            End If
     
            Dim objLivreSel As Fiche_Du_Livre = CType(cboBoxInsertBefore.SelectedItem, Fiche_Du_Livre)
            Dim noeudSel As LinkedListNode(Of Fiche_Du_Livre) = lienLivre.Find(objLivreSel)
            lienLivre.AddBefore(noeudSel, nouvOuvrage)
            Call FillDataGridView()
            cboBoxInsertBefore.Enabled = False
            Call ClearTextBox()
        End Sub
     
        Private Sub cboBoxInsertAfter_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboBoxInsertAfter.SelectedIndexChanged
            'Inserer nouvel ouvrage apres -x-
            Dim nouvOuvrage As Fiche_Du_Livre = New Fiche_Du_Livre
     
            nouvOuvrage.Genre = txtGenre.Text
            nouvOuvrage.Auteur = txtAuteur.Text
            nouvOuvrage.Titre = txtTitre.Text
            If IsNumeric(txtID.Text) Then
                nouvOuvrage.ID = Integer.Parse(txtID.Text)
            Else
                MessageBox.Show("Err  non Numerique...")
                txtID.Focus()
            End If
            If IsNumeric(txtDisponibilte.Text) Then
                nouvOuvrage.Disponibilite = Integer.Parse(txtDisponibilte.Text)
            Else
                MessageBox.Show("Err non Numerique...")
                txtDisponibilte.Focus()
            End If
     
            Dim objLivreSel As Fiche_Du_Livre = CType(cboBoxInsertAfter.SelectedItem, Fiche_Du_Livre)
            Dim noeudSel As LinkedListNode(Of Fiche_Du_Livre) = lienLivre.Find(objLivreSel)
            lienLivre.AddAfter(noeudSel, nouvOuvrage)
            Call FillDataGridView()
            Call ClearTextBox()
            cboBoxInsertAfter.Enabled = False
        End Sub
        Private Sub cboBoxRemove_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboBoxRemove.SelectedIndexChanged
            Call ClearTextBox()
            'Supprime l'ouvrage selectionne -x-
            Dim objLivreSel As Fiche_Du_Livre = CType(cboBoxInsertBefore.SelectedItem, Fiche_Du_Livre)
            txtGenre.Text = objLivreSel.Genre
            txtAuteur.Text = objLivreSel.Auteur
            txtTitre.Text = objLivreSel.Titre
            txtID.Text = objLivreSel.ID.ToString
            txtDisponibilte.Text = objLivreSel.Disponibilite.ToString
            lienLivre.Remove(objLivreSel)
            Call FillDataGridView()
        End Sub
        Private Sub btnClearTextBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearTextBox.Click
            Call ClearTextBox()
        End Sub
        Private Sub FillDataGridView()
            Dim tableAffichage(lienLivre.Count) As Fiche_Du_Livre
            lienLivre.CopyTo(tableAffichage, 0)
            DataGridView1.DataSource = tableAffichage
            DataGridView1.Refresh()
     
            For Each obj As Fiche_Du_Livre In lienLivre
                cboBoxInsertBefore.Items.Add(obj)
                cboBoxInsertAfter.Items.Add(obj)
            Next
            DataGridView1.Update()
            DataGridView1.Refresh()
        End Sub
     
        Private Sub ClearTextBox()
            Me.txtGenre.Text = ""
            Me.txtAuteur.Text = ""
            Me.txtTitre.Text = ""
            Me.txtID.Text = ""
            Me.txtDisponibilte.Text = ""
        End Sub
     
        Private Sub btnEnregFichier_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnregFichier.Click
            Dim fichierChemin As String = ""
            Me.SaveFileDialog1.Filter = "Fichier Texte(*.txt)|*.txt"
            If Me.SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                fichierChemin = Me.SaveFileDialog1.FileName
                If Len(fichierChemin) = 0 Then
                    MessageBox.Show("erreur fichier...")
                    Exit Sub
                Else
                    Call EcritFichier(fichierChemin)
     
                End If
            End If
        End Sub
        Private Sub EcritFichier(ByVal cheminfichier As String)
            Dim MonWriter As System.IO.StreamWriter = New System.IO.StreamWriter(cheminfichier, False)
            Dim strLigne As String = ""
            Dim sep As String = ","
            Try
                For Each objLivre In lienLivre
                    strLigne = objLivre.Genre & sep & objLivre.Auteur & objLivre.Titre & sep & objLivre.ID.ToString & sep & objLivre.Titre
                    MonWriter.WriteLine(strLigne)
                Next
                MonWriter.Close()
            Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
                MsgBox("Ligne " & ex.Message & _
                "non valide,sera saute....")
            End Try
        End Sub
     
     
        Private Sub btnValider_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnValider.Click
            If txtGenre.Text <> "" And txtAuteur.Text <> "" _
           And txtTitre.Text <> "" And txtID.Text <> "" _
           And txtDisponibilte.Text <> "" Then
                cboBoxInsertBefore.Enabled = True
                cboBoxInsertAfter.Enabled = True
            End If
        End Sub
    End Class
    'La classe Fiche_Du_Livre
    Public Class Fiche_Du_Livre
        'Dim Genre As String
        'Dim Auteur As String
        'Dim Titre As String
        'Dim ID As Integer
        'Dim Disponibilite As Integer
        Private m_Genre As String
        Public Property Genre() As String
            Get
                Return m_Genre
            End Get
            Set(ByVal value As String)
                m_Genre = value
            End Set
        End Property
     
        Private m_Auteur As String
        Public Property Auteur() As String
            Get
                Return m_Auteur
            End Get
            Set(ByVal value As String)
                m_Auteur = value
            End Set
        End Property
        Private m_Titre As String
        Public Property Titre() As String
            Get
                Return m_Titre
            End Get
            Set(ByVal value As String)
                m_Titre = value
            End Set
        End Property
        Private m_ID As Integer
        Public Property ID() As Integer
            Get
                Return m_ID
            End Get
            Set(ByVal value As Integer)
                m_ID = value
            End Set
        End Property
        Private m_Disponibilite As String
        Public Property Disponibilite() As String
            Get
                Return m_Disponibilite
            End Get
            Set(ByVal value As String)
                m_Disponibilite = value
            End Set
        End Property
     
    End Class
    pj: fichier rar du projet
    bon code....

Discussions similaires

  1. Liste des fichiers *.txt
    Par tapiou dans le forum Débuter
    Réponses: 12
    Dernier message: 28/12/2011, 12h38
  2. Problème Gestion Liste et fichiers txt
    Par Freud44 dans le forum Général Java
    Réponses: 2
    Dernier message: 26/08/2008, 21h10
  3. [VBA-E] macro ouverture liste de fichier txt
    Par didi73 dans le forum Macros et VBA Excel
    Réponses: 3
    Dernier message: 16/03/2007, 14h30
  4. Réponses: 7
    Dernier message: 06/11/2005, 10h00
  5. [LG]liste chainée et fichier
    Par grand's dans le forum Langage
    Réponses: 5
    Dernier message: 10/05/2004, 21h25

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo