none
reading txt file and converting to single RRS feed

  • Question

  • I have a .txt file like this:

    -23.5     56.2     3.4

    3.6       -23.5     -2

    .              .          .

    I want to read each data in an array dimensioned as single, to get:

    x(0)=-23.5,  x(1)=56.2 ...............

    In VB6 was easy, but i do not know the best way to do this in visual studio.

    Saturday, March 21, 2020 3:47 PM

All replies

  • Hi

    Assuming the text file contents are exactly as you show, then try this stand alone example:

    Option Strict On
    Option Explicit On
    Public Class Form1
      ' my file location
      Dim path As String = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "Test1.txt")
      Dim x() As Single
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim lines() As String = IO.File.ReadAllLines(path)
        Dim lst As New List(Of Single)
        For Each ln As String In lines
          Dim a() As String = ln.Split(New String() {" "c}, StringSplitOptions.RemoveEmptyEntries)
          For i As Integer = 0 To a.Length - 1
            lst.Add(CSng(a(i)))
          Next
        Next
        x = lst.ToArray
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    Saturday, March 21, 2020 4:24 PM
  • Thanks Leshay, i will try.

    In VB& it was very easy (example):

    dim x(100) as single

    Open file_address For Input As #1

    for i=0 to 99

    Input #1, x(i)

    next i

    close #1

    Saturday, March 21, 2020 7:08 PM
  • Thanks Leshay, i will try.

    In VB& it was very easy (example):

    dim x(100) as single

    Open file_address For Input As #1

    for i=0 to 99

    Input #1, x(i)

    next i

    close #1

    That may work but you should move to .NET code rather than old VB6 code that was brought forward to assist VB6 developer move to .NET.

    Les's example would be the .NET way. Sure it's more code yet that does not mean it's not a valid choice. But if you want to stick with out of date code that is up to you.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, March 22, 2020 12:31 AM
  • Nice Karen,

    i'm moving to Visual Studio all VB6 projects (many).

    Sometimes i find visual studio not much intuitive. One cannot reach the solution without asking around.

    Hy, ciao.

    Sunday, March 22, 2020 4:43 PM
  • Nice Karen,

    i'm moving to Visual Studio all VB6 projects (many).

    Sometimes i find visual studio not much intuitive. One cannot reach the solution without asking around.


    Just try it a little bit more. Be aware that VB6 was enclosed in Visual Studio '98. But the possibilities with that were so limited. (Desktop, Windows Service and that was it and then even only with Win32 controls and OleDB). 

    After a while you don't want to go back anymore. Not mine opinion something what is proven. :-)


    Success
    Cor

    Sunday, March 22, 2020 7:47 PM