OVH Cloud OVH Cloud

lire un fichier

3 réponses
Avatar
eric
Bonjour,

je voudrais lire une fichier texte ligne par ligne et ajouter chaque ligne
ds une combobox. comment dois-je m'y prendre?

3 réponses

Avatar
ng
Bonjour,
Comme ceci :

Dim k as integer, Buff as String
k=freefile
open "c:mon fichier.txt" for input as #k
do while not eof(k)
line input #k,Buff
combo1.additem Buff
loop
close #k


Nicolas.
"eric" a écrit dans le message de news:

Bonjour,

je voudrais lire une fichier texte ligne par ligne et ajouter chaque ligne
ds une combobox. comment dois-je m'y prendre?




Avatar
François Picalausa
Bonjour/soir,

Une solution consisterait à lire tout le fichier en binaire, le "splitter"
et lire le tableau dans le combobox:
Dim strFileContent As String, FFN As Integer, strTblLines() As String, i
As Long

FFN = FreeFile

Open "c:windowswinhelp.exe" For Binary As FFN
strFileContent = String$(LOF(FFN), Chr$(0))
Get FFN, , strFileContent
Close FFN

strTblLines = Split(strFileContent, vbCrLf)

For i = 0 To UBound(strTblLines)
Combo1.AddItem strTblLines(i)
Next i

Sinon, Open for Input puis Line Input...
--
François Picalausa (MVP VB)
FAQ VB : http://faq.vb.free.fr
MSDN : http://msdn.microsoft.com


eric wrote:
Bonjour,

je voudrais lire une fichier texte ligne par ligne et ajouter chaque
ligne ds une combobox. comment dois-je m'y prendre?


Avatar
François Picalausa
Bonjour/soir,

Je viens de faire un bench pour tester quelle méthode était la plus rapide:

Sub Main()
Dim StopWatch As CStopWatch
Dim i As Long
Const LOOP_COUNT As Long = 50
Const FileName As String = "C:WINDOWSKB823559.log"
Dim tl As Long
Dim t1 As Long
Dim t2 As Long
Dim cboCombo As ComboBox

Set cboCombo = Form1.Combo1

Set StopWatch = New CStopWatch

StopWatch.Reset
For i = 1 To LOOP_COUNT
Next i
tl = StopWatch.Elapsed

StopWatch.Reset
For i = 1 To LOOP_COUNT
AddFileLinesToCbo FileName, cboCombo
Next i
t1 = StopWatch.Elapsed


StopWatch.Reset
For i = 1 To LOOP_COUNT
AddFileLinesToCbo2 FileName, cboCombo
Next i
t2 = StopWatch.Elapsed

Set StopWatch = Nothing
Set cboCombo = Nothing
Unload Form1

Debug.Print "Loop time : " & tl & " ms"
Debug.Print "AddFileLinesToCbo : " & t1 & " ms"
Debug.Print "AddFileLinesToCbo2 : " & t2 & " ms"
End Sub

Sub AddFileLinesToCbo(strFile As String, ComboBox As ComboBox)
Dim strFileContent As String, FFN As Integer, strTblLines() As String, i
As Long

ComboBox.Clear

FFN = FreeFile

Open strFile For Binary As FFN
strFileContent = String$(LOF(FFN), Chr$(0))
Get FFN, , strFileContent
Close FFN

strTblLines = Split(strFileContent, vbCrLf)

For i = 0 To UBound(strTblLines)
ComboBox.AddItem strTblLines(i)
Next i
End Sub

Sub AddFileLinesToCbo2(strFile As String, ComboBox As ComboBox)
Dim k As Integer, Buff As String

ComboBox.Clear

k = FreeFile
Open strFile For Input As #k
Do While Not EOF(k)
Line Input #k, Buff
ComboBox.AddItem Buff
Loop
End Sub

Loop time : 0 ms
AddFileLinesToCbo : 5119 ms
AddFileLinesToCbo2 : 5650 ms

Ma méthode est plus gourmande en mémoire mais économise en temps...

--
François Picalausa (MVP VB)
FAQ VB : http://faq.vb.free.fr
MSDN : http://msdn.microsoft.com


ng wrote:
Bonjour,
Comme ceci :

Dim k as integer, Buff as String
k=freefile
open "c:mon fichier.txt" for input as #k
do while not eof(k)
line input #k,Buff
combo1.additem Buff
loop
close #k


Nicolas.
"eric" a écrit dans le message de news:

Bonjour,

je voudrais lire une fichier texte ligne par ligne et ajouter chaque
ligne ds une combobox. comment dois-je m'y prendre?