PROBLEMAS PARA LEER *TXT LINEA A LINEA
necesito que al leer desde un archivo de texto unas coordenadas. el archivo de texto contiene los datos de la forma siguiente:
4
2
4290
15525
13055
15570
4311
6861
8676
6876
el problema esta en que todos estos datos deben introducirse en una matriz cuya dimension seria el doble del numero de puntos (numero puntos = n):
por ejemplo, para el *txt anterior el n=5 (porque son 2 lineas por cada punto)
El codigo que he utilizado es el siguiente:
Private Sub darchci_Click()
CommonDialog1.Filter = "Archivos de Texto (*.txt)|*.txt" ' ABRE VENTANA PARA los datos (SOLO LECTURA)
CommonDialog1.FilterIndex = 1
CommonDialog1.ShowOpen
If CommonDialog1.FileName = "" Then
Exit Sub 'SI NO HAY datos NO CARGA NADA
Else ' cargamos el archivo
ruta = CommonDialog1.FileName
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FileExists(ruta) Then Exit Sub ' si no existe salimos
Set ofich = fso.opentextfile(ruta, 1) 'abre el fichero seleccionado para su lectura
Do While ofich.atendofstream = False 'lee linea por linea el fichero
txt = ofich.readline
Loop
ReDim mat_txt(1 To 2 * n) 'matriz donde se ubicara lo leido en formato numerico
ReDim puntos(0 To 2 * n - 2)
puntos() = mat_txt(CLng(txt))
Frame2.Visible = True
contador = 0
For fila = 0 To (2 * n - 2) Step 2
contador = contador
Text2(contador).Enabled = True
Text2(contador).Visible = True
Text2(fila).Text = puntos(fila)
Text3(contador).Enabled = True
Text3(contador).Visible = True
Text3(fila + 1).Text = puntos(fila + 1)
contador = contador + 1
Next
End If
ofich.Close ' cerramos el objeto
End Sub
aparentemente creo que funciona bien hasta llegar a
puntos() = mat_txt(CLng(txt))
donde me dice que "error6. .. el subindice esta fuera del intervalo"
que es lo que falla?
gracias x anticipado y salu2 a todos
4
2
4290
15525
13055
15570
4311
6861
8676
6876
el problema esta en que todos estos datos deben introducirse en una matriz cuya dimension seria el doble del numero de puntos (numero puntos = n):
por ejemplo, para el *txt anterior el n=5 (porque son 2 lineas por cada punto)
El codigo que he utilizado es el siguiente:
Private Sub darchci_Click()
CommonDialog1.Filter = "Archivos de Texto (*.txt)|*.txt" ' ABRE VENTANA PARA los datos (SOLO LECTURA)
CommonDialog1.FilterIndex = 1
CommonDialog1.ShowOpen
If CommonDialog1.FileName = "" Then
Exit Sub 'SI NO HAY datos NO CARGA NADA
Else ' cargamos el archivo
ruta = CommonDialog1.FileName
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FileExists(ruta) Then Exit Sub ' si no existe salimos
Set ofich = fso.opentextfile(ruta, 1) 'abre el fichero seleccionado para su lectura
Do While ofich.atendofstream = False 'lee linea por linea el fichero
txt = ofich.readline
Loop
ReDim mat_txt(1 To 2 * n) 'matriz donde se ubicara lo leido en formato numerico
ReDim puntos(0 To 2 * n - 2)
puntos() = mat_txt(CLng(txt))
Frame2.Visible = True
contador = 0
For fila = 0 To (2 * n - 2) Step 2
contador = contador
Text2(contador).Enabled = True
Text2(contador).Visible = True
Text2(fila).Text = puntos(fila)
Text3(contador).Enabled = True
Text3(contador).Visible = True
Text3(fila + 1).Text = puntos(fila + 1)
contador = contador + 1
Next
End If
ofich.Close ' cerramos el objeto
End Sub
aparentemente creo que funciona bien hasta llegar a
puntos() = mat_txt(CLng(txt))
donde me dice que "error6. .. el subindice esta fuera del intervalo"
que es lo que falla?
gracias x anticipado y salu2 a todos
hola maya, he probado tu código y lo he retocado para que haga lo que quieres, compruébalo y fíjate en los cambios:
Private Sub darchci_Click()
CommonDialog1.Filter = "Archivos de Texto (*.txt)|*.txt" ' ABRE VENTANA PARA los datos (SOLO LECTURA)
CommonDialog1.FilterIndex = 1
CommonDialog1.ShowOpen
If CommonDialog1.FileName = "" Then
Exit Sub 'SI NO HAY datos NO CARGA NADA
Else ' cargamos el archivo
ruta = CommonDialog1.FileName
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FileExists(ruta) Then Exit Sub ' si no existe salimos
Set ofich = fso.opentextfile(ruta, 1) 'abre el fichero seleccionado para su lectura
Dim contador_dimension As Long
contador_dimension = 0
Dim puntos() As Long
Do While ofich.atendofstream = False 'lee linea por linea el fichero
txt = ofich.readline
If txt <> "" And IsNumeric(txt) Then
ReDim Preserve puntos(0 To contador_dimension) As Long
puntos(contador_dimension) = CLng(txt)
contador_dimension = contador_dimension + 1
End If
Loop
n = contador_dimension / 2
Frame2.Visible = True
contador = 0
For fila = 0 To (2 * n - 1) Step 2
Text2(contador).Enabled = True
Text2(contador).Visible = True
Text2(contador).Text = puntos(fila)
Text3(contador).Enabled = True
Text3(contador).Visible = True
Text3(contador).Text = puntos(fila + 1)
contador = contador + 1
Next
End If
ofich.Close ' cerramos el objeto
End Sub
Un saludo, hasta otra
Private Sub darchci_Click()
CommonDialog1.Filter = "Archivos de Texto (*.txt)|*.txt" ' ABRE VENTANA PARA los datos (SOLO LECTURA)
CommonDialog1.FilterIndex = 1
CommonDialog1.ShowOpen
If CommonDialog1.FileName = "" Then
Exit Sub 'SI NO HAY datos NO CARGA NADA
Else ' cargamos el archivo
ruta = CommonDialog1.FileName
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FileExists(ruta) Then Exit Sub ' si no existe salimos
Set ofich = fso.opentextfile(ruta, 1) 'abre el fichero seleccionado para su lectura
Dim contador_dimension As Long
contador_dimension = 0
Dim puntos() As Long
Do While ofich.atendofstream = False 'lee linea por linea el fichero
txt = ofich.readline
If txt <> "" And IsNumeric(txt) Then
ReDim Preserve puntos(0 To contador_dimension) As Long
puntos(contador_dimension) = CLng(txt)
contador_dimension = contador_dimension + 1
End If
Loop
n = contador_dimension / 2
Frame2.Visible = True
contador = 0
For fila = 0 To (2 * n - 1) Step 2
Text2(contador).Enabled = True
Text2(contador).Visible = True
Text2(contador).Text = puntos(fila)
Text3(contador).Enabled = True
Text3(contador).Visible = True
Text3(contador).Text = puntos(fila + 1)
contador = contador + 1
Next
End If
ofich.Close ' cerramos el objeto
End Sub
Un saludo, hasta otra
