[Lecture fichier] en VB

Utilisateur anonyme -  
 Utilisateur anonyme -
Bonjour,

Voici mon soucis:

J'aimerais lire un fichier texte et commencer la lecture de celui ci au moment ou une ligne comporte un "A" et pour chaque ligne récupérer dans un tableau les caractère se trouvant avant l'espace et après.

Voici un exemple de fichier à lire:
date:..
auteur:kjdjh


kks


A122 64
A3 889

dans ce fichier j'aimerais récupérer dans un tableau :
122 et 64 puis 3 et 889

J'ai déjà fais un essai mais pas concluant:

Dim nombre As Integer
Public Sub LireFichier()
Form3.c.ShowOpen
Form3.Text1.Text = Form3.c.FileName
Dim i, nombre As Integer
Dim texte()
i = -1
Open Form3.c.FileName For Input As #1
While Not EOF(1)
i = i + 1
ReDim Preserve texte(i)
Line Input #1, texte(i)
Wend
Close #1

Dim j As Integer
Dim premier, reponse
'For Each loic In texte()
'If InStr(loic, "M") Then
'texte().Remove loic
'End If
'Next
'Dim texte
j = -1
While j <= 5
j = j + 1
premier = texte(j)
reponse = InStr(1, premier, "A", vbTextCompare)
If reponse = 1 Then
j = j + 1
End If
MsgBox premier
Wend
End Sub


Puis-je avoir de l'aide s'il vous plait ?
Configuration: Windows XP
Firefox 2.0.0.9

2 réponses

  1. Utilisateur anonyme
     
    Bonjour,

    voilà, j'ai déboggé votre code, j'ai bien sur, apporter des modifications
    pour qu'il fasse bien ce qu'il doit faire, enfin je crois :-)

    Public Sub LireFichier()
    
        Dim Nombre As Integer
        Dim numFileIN As Long, numFileOUT As Long
        
        Dim Texte() As String, i As Long
        Dim TexteResultat() As String, k As Long
        
        Dim j As Integer, Resultat As String
        Dim Premier As String, Second As String
        Dim Reponse As Long
        
        numFileOUT = FreeFile
        'Form3.c.ShowOpen
        'Form3.Text1.Text = Form3.c.FileName
        numFileIN = FreeFile
        i = -1
        Open "C:\MonFichier.txt" For Input As #numFileIN
        While Not EOF(numFileIN)
            i = i + 1
            ReDim Preserve Texte(i)
            Line Input #1, Texte(i)
        Wend
        Close numFileIN
        
        'j = -1
        k = 0
        For j = 0 To UBound(Texte)
        'While j <= 5
            'j = j + 1
            Premier = Texte(j)
            Reponse = InStr(1, Premier, "A", vbBinaryCompare)
            If (Reponse > 0) Then
                k = (k + 1)
                ReDim Preserve TexteResultat(k)
                Resultat = Mid(Premier, (Reponse + 1))
                TexteResultat(k - 1) = Resultat
            End If
        'Wend
        Next j
    
        Open "C:\Resultat.txt" For Output As #numFileOUT
        
        For j = 0 To UBound(TexteResultat)
            Premier = TexteResultat(j)
            Reponse = InStr(1, Premier, " ", vbTextCompare)
            If (Reponse > 0) Then
                Resultat = Mid(Premier, 1, (Reponse - 1))
                Write #numFileOUT, Resultat
                Second = Second & vbLf & Resultat
                Resultat = Mid(Premier, (Reponse + 1))
                Write #numFileOUT, Resultat
                Second = Second & vbLf & Resultat
            End If
        Next j
    
       Close numFileOUT
    
        MsgBox Second & vbLf & "Voir fichier Resultat.txt"
    
    End Sub
    '
    


    suggestions :

    je vous recommande l'utilisaton du mot clé "FreeFile"
    pour adresser vos fichiers, pour de gros fichiers, vous
    pourrer lire et écrire sans confusion de numéro de fichier
    dans la même boucle.

    Pour adresser un tableau, utiliser le mot clé "Ubound",
    très simple il renvoie toujours l'index du dernier élément.

    Lupin
    1
  2. Utilisateur anonyme
     
    personne ne peut m'aider ?
    0