Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

File accessing and array storing

2 réponses
Avatar
Gilles
Hi all
I have got a CSV file containing datas I should use for programming in
Autocad.
This data is multiple for each object.

For example I want to create a "layer":
This layer needs a few informations to be valid
-name
-color
-plotstyle
-...

My CSV file has all these datas but what I would like to do is retrieve the
data in my CSV line by line and store them in an array so I could create a
function which would be able to retrieve all the necessary datas in this
array like this "public function Create_Layer (name as string, color as
integer, plotstyle as string,...)"

Any ideas or suggestions???



Code I use now (it doesn't work properly yet...)
Dim arrTitle As Variant
Dim i As Integer

Public Function Read_File(strIndex As String, lngIndex As Long)

Dim strTitle As String
Dim intFile As Integer

'get the next free file number
intFile = FreeFile

'open the text file
Open strFileOrigin For Input As #intFile

'until the end of file
While Not EOF(intFile)

'read the line and store it in a variable
Line Input #intFile, strTitle

'isolate the "lngIndex" first characters and test
If Left$(strTitle, lngIndex) = strIndex Then

'remove the "lngIndex" first characters
strTitle = Right$(strTitle, (Len(strTitle) - lngIndex))

'remove the ; character
strTitle = Replace(strTitle, ";", "")
i = i + 1 ' Here I should count the size
of my array.....
ReDim arrTitle(i) ' Here I should store the
datas.....
arrTitle = arrTitle + strTitle ' Here I should store the datas.....

'Debug.Print strTitle
'end if
End If

'loop
Wend

'close the file
Close #intFile

End Function

2 réponses

Avatar
Gilles
Faut une traduction ou tout le monde a compris parce que a force de discuter
sur des forums dans 36 langues, on s'y perd...

:-)

Gilles

"Gilles" a écrit dans le message de
news:
Hi all
I have got a CSV file containing datas I should use for programming in
Autocad.
This data is multiple for each object.

For example I want to create a "layer":
This layer needs a few informations to be valid
-name
-color
-plotstyle
-...

My CSV file has all these datas but what I would like to do is retrieve


the
data in my CSV line by line and store them in an array so I could create a
function which would be able to retrieve all the necessary datas in this
array like this "public function Create_Layer (name as string, color as
integer, plotstyle as string,...)"

Any ideas or suggestions???



Code I use now (it doesn't work properly yet...)
Dim arrTitle As Variant
Dim i As Integer

Public Function Read_File(strIndex As String, lngIndex As Long)

Dim strTitle As String
Dim intFile As Integer

'get the next free file number
intFile = FreeFile

'open the text file
Open strFileOrigin For Input As #intFile

'until the end of file
While Not EOF(intFile)

'read the line and store it in a variable
Line Input #intFile, strTitle

'isolate the "lngIndex" first characters and test
If Left$(strTitle, lngIndex) = strIndex Then

'remove the "lngIndex" first characters
strTitle = Right$(strTitle, (Len(strTitle) - lngIndex))

'remove the ; character
strTitle = Replace(strTitle, ";", "")
i = i + 1 ' Here I should count the


size
of my array.....
ReDim arrTitle(i) ' Here I should store the
datas.....
arrTitle = arrTitle + strTitle ' Here I should store the datas.....

'Debug.Print strTitle
'end if
End If

'loop
Wend

'close the file
Close #intFile

End Function




Avatar
ng
Salut,

Tu pourrais faire ca avec un type utilisateur :

'//Dans un module :
Public Type tLayerInfos
strName As String
strColor As String '//ou long, ca dépend du format de la couleur
strPlotStyle As String
End Type

Public Function GetLayerArrayFromCSV(strCSVPath As String) As tLayerInfos()
Dim k As Integer, strBuff As String, tblLines() As String
Dim tblInfos() As String, boolInt As Boolean, tblOut() As tLayerInfos
Dim intIndex As Integer
k = FreeFile
Open strCSVPath For Binary As #k
strBuff = String$(LOF(k), vbNullChar)
Get #k, , strBuff
Close #k
tblLines = Split(strBuff, vbCrLf, , vbTextCompare): strBuff = ""
For k = 0 To UBound(tblLines)
If tblLines(k) Like "*;*;*" Then
tblInfos = Split(tblLines(k), ";", , vbTextCompare)
If Not boolInt Then
boolInt = True
intIndex = 0
Else
intIndex = UBound(tblOut) + 1
End If
ReDim Preserve tblOut(intIndex)
tblOut(intIndex).strName = tblInfos(0)
tblOut(intIndex).strColor = tblInfos(1)
tblOut(intIndex).strPlotStyle = tblInfos(2)
End If
Next
GetLayerArrayFromCSV = tblOut
Erase tblInfos
Erase tblLines
Erase tblOut
End Function

'//Dans to form :
Private Sub Form_Load()
Dim tblLayers() As tLayerInfos, i As Integer
tblLayers = GetLayerArrayFromCSV("c:1.csv")
For i = 0 To UBound(tblLayers)
Debug.Print "Name : " & tblLayers(i).strName & " Color : " &
tblLayers(i).strColor & " Plot : " & tblLayers(i).strPlotStyle
Next
End Sub


--
Nicolas G.
FAQ VB : http://faq.vb.free.fr
API Guide : http://www.allapi.net
Google Groups : http://groups.google.fr/
MZ-Tools : http://www.mztools.com/
http://apisvb.europe.webmatrixhosting.net/

Gilles a écrit :

Hi all
I have got a CSV file containing datas I should use for programming in
Autocad.
This data is multiple for each object.

For example I want to create a "layer":
This layer needs a few informations to be valid
-name
-color
-plotstyle
-...

My CSV file has all these datas but what I would like to do is
retrieve the data in my CSV line by line and store them in an array
so I could create a function which would be able to retrieve all the
necessary datas in this array like this "public function Create_Layer
(name as string, color as integer, plotstyle as string,...)"

Any ideas or suggestions???



Code I use now (it doesn't work properly yet...)
Dim arrTitle As Variant
Dim i As Integer

Public Function Read_File(strIndex As String, lngIndex As Long)

Dim strTitle As String
Dim intFile As Integer

'get the next free file number
intFile = FreeFile

'open the text file
Open strFileOrigin For Input As #intFile

'until the end of file
While Not EOF(intFile)

'read the line and store it in a variable
Line Input #intFile, strTitle

'isolate the "lngIndex" first characters and test
If Left$(strTitle, lngIndex) = strIndex Then

'remove the "lngIndex" first characters
strTitle = Right$(strTitle, (Len(strTitle) - lngIndex))

'remove the ; character
strTitle = Replace(strTitle, ";", "")
i = i + 1 ' Here I should count
the size of my array.....
ReDim arrTitle(i) ' Here I should store the
datas.....
arrTitle = arrTitle + strTitle ' Here I should store the
datas.....

'Debug.Print strTitle
'end if
End If

'loop
Wend

'close the file
Close #intFile

End Function