将数组写入文件 [英] writing an array to a file

查看:82
本文介绍了将数组写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Visual Basic的新手,对编程来说是新手.我正在尝试在专有数据分析软件包中编写宏.我已经创建了要写入.txt文件的2D数组,因此每个值都是分隔的, 按照我最初定义的结构排列的数组.该数组为40行x 4列,并包含整数值.我该怎么做?我已经在寻找解决方案,但没有找到任何一致的方法,这一切都让人感到困惑.

I am new to Visual Basic and relatively new to programing.  I am trying to write a macro within a proprietary data analysis software package.  I have created a 2D array that I want to write to a .txt file, so each value is separated and the array structured as I originally defined.  The array is 40 rows x 4 columns, and contains integers values.  How do I do this?  I have searched for a solution, but haven't found any consistent approach, which is all a bit confusing.

谢谢.

Alex.

推荐答案

Hi

这是一种方法.它将逗号分隔的数据写入与可执行文件位于同一文件夹中的文件.这是非常基本的代码,没有任何异常处理.

Here is one way. It writes comma delimited data to a file located in the same folder as the executable. This is very basic code and doesn't have any exception handling.

Option Strict On
Option Infer Off
Option Explicit On
Public Class Form1
    Dim myArray(39, 3) As Integer
    Dim r As New Random
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        ' some dummy data
        For i As Integer = 0 To 39
            For j As Integer = 0 To 3
                myArray(i, j) = r.Next(99, 999)
            Next
        Next
        ' first, write data to file
        WriteFile()

        Stop ' click continue to read back data

        '  read data fromfile
        ReadFile()
    End Sub

    ' write myArray data to file
    Public Sub WriteFile()
        Dim path As String = Application.StartupPath & "\MyArray.txt"
        Using sr As IO.StreamWriter = New IO.StreamWriter(path, False)
            For i As Integer = 0 To 39
                For j As Integer = 0 To 2
                    sr.Write(myArray(i, j).ToString & ",")
                Next
                sr.WriteLine(myArray(i, 3).ToString)
            Next
        End Using
    End Sub

    ' obviously,file must already be written before using this
    Public Sub ReadFile()
        Dim path As String = Application.StartupPath & "\MyArray.txt"
        Dim line As String = Nothing
        Dim row As Integer = 0
        Using sr As IO.StreamReader = New IO.StreamReader(path)
            Do
                line = sr.ReadLine()
                If Not line = Nothing Then
                    Dim a() As String = Split(line, ",")
                    myArray(row, 0) = GetInteger(a(0))
                    myArray(row, 1) = GetInteger(a(1))
                    myArray(row, 2) = GetInteger(a(2))
                    myArray(row, 3) = GetInteger(a(3))
                    row += 1
                End If
            Loop Until line = Nothing
        End Using
    End Sub

    ' function to return a valid integer from string
    Private Function GetInteger(s As String) As Integer
        Dim v As Integer = 0
        If Integer.TryParse(s, v) Then Return v
        Return 0
    End Function
End Class


这篇关于将数组写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆