-->

Writing a matrix to a text file

2019-06-14 15:18发布

问题:

So i have done how to read a matrix from a text file where the first line defines the elements, however my question is how would i do the opposite that would write to the text file. I would like it to ask the user for the row 0 column 0, and add on, and have this code to write into the console, but do not know how to do into a text file

This is the code to write into console:

Dim size As Integer = 3
Dim numberWidth As Integer = 2
Dim format As String = "D" & numberWidth

Dim A(size - 1, size - 1) As Integer
For i As Integer = 0 To A.GetUpperBound(0)
    For j As Integer = 0 To A.GetUpperBound(1)
        Console.Write(String.Format("Enter The Matrix Element at A[Row {0}, Col {1}]: ", i, j))
        A(i, j) = Convert.ToInt16(Console.ReadLine())
    Next
Next

This is code for reading the matrix

Dim path = "d:\matrix.txt"
Dim A(,) As Integer
Using reader As New IO.StreamReader(path)
    Dim size = reader.ReadLine() ' read first line which is the size of the matrix (assume the matrix is a square)
    Redim A(size - 1, size - 1)

    Dim j = 0 ' the current line in the matrix
    Dim line As String = reader.ReadLine() ' read next line
    Do While (line <> Nothing) ' loop as long as line is not empty
        Dim numbers = line.Split(" ") ' split the numbers in that line
        For i = 0 To numbers.Length - 1
            A(j, i) = numbers(i) ' copy the numbers into the matrix in current line
        Next

        j += 1 ' increment the current line
        line = reader.ReadLine() ' read next line
    Loop
End Using

Console.WriteLine("Matrix A :")
Dim numberWidth As Integer = 2
Dim format As String = "D" & numberWidth
For i As Integer = 0 To A.GetUpperBound(0)
    Console.Write("| ")
    For j As Integer = 0 To A.GetUpperBound(1)
        Console.Write("{0} ", A(i, j).ToString(format))
    Next
    Console.WriteLine("|")
Next

Save function:

    Dim A(,) As Integer
Sub SaveMatrix()
    Dim path As String = "z:\matrix.txt"







    Using fs As New System.IO.FileStream(path, IO.FileMode.OpenOrCreate)
        fs.SetLength(0) ' reset file length to 0 in case we are overwriting an existing file
        Using sw As New System.IO.StreamWriter(fs)
            Dim line As New System.Text.StringBuilder
            sw.WriteLine((A.GetUpperBound(0) + 1).ToString()) ' size of array in first line
            For i As Integer = 0 To A.GetUpperBound(0)
                line.Clear()
                For j As Integer = 0 To A.GetUpperBound(1)
                    line.Append(A(i, j).ToString() & " ")
                Next
                sw.WriteLine(line.ToString().Trim()) ' output each row to the file
            Next
        End Using
    End Using
    Console.WriteLine("")
    Console.WriteLine("Matrix successfully saved to:")
    Console.WriteLine(path)


End Sub

回答1:

Here's a framework to build upon demonstrating how to enter, save, and load your matrices. You can add other options to the menu based on what other operations you need to implement:

Module Module1

    Public A(,) As Integer

    Public Sub Main()
        Dim response As String
        Dim quit As Boolean = False
        Do
            Console.WriteLine("")
            Console.WriteLine("--- Menu Options ---")
            Console.WriteLine("1) Enter a Matrix")
            Console.WriteLine("2) Display Matrix")
            Console.WriteLine("3) Save Matrix")
            Console.WriteLine("4) Load Matrix")
            Console.WriteLine("5) Quit")
            Console.Write("Menu choice: ")
            response = Console.ReadLine()
            Console.WriteLine("")
            Select Case response
                Case "1"
                    EnterMatrix()

                Case "2"
                    DisplayMatrix()

                Case "3"
                    SaveMatrix()

                Case "4"
                    LoadMatrix()

                Case "5"
                    quit = True

                Case Else
                    Console.WriteLine("")
                    Console.WriteLine("Invalid Menu Choice")

            End Select
        Loop While (Not quit)

        Console.Write("Press any key to dismiss the console...")
        Console.ReadKey()
    End Sub

    Public Sub EnterMatrix()
        Dim valid As Boolean
        Dim size, value As Integer
        Console.Write("Enter the Size of the Square Matrix: ")
        Dim response As String = Console.ReadLine()
        If Integer.TryParse(response, size) Then
            If size >= 2 Then
                Console.WriteLine("")
                ReDim A(size - 1, size - 1)
                For i As Integer = 0 To A.GetUpperBound(0)
                    Console.WriteLine("--- Row " & i & " ---")
                    For j As Integer = 0 To A.GetUpperBound(1)
                        valid = False
                        Do
                            Console.Write(String.Format("Enter The Matrix Element at A[Row {0}, Col {1}]: ", i, j))
                            response = Console.ReadLine()
                            If Integer.TryParse(response, value) Then
                                A(i, j) = value
                                valid = True
                            Else
                                Console.WriteLine("")
                                Console.WriteLine("Matrix Element must be a valid Integer!")
                                Console.WriteLine("")
                            End If
                        Loop While (Not valid)
                    Next
                    Console.WriteLine("")
                Next
            Else
                Console.WriteLine("")
                Console.WriteLine("Size must be greater than or equal to 2!")
            End If
        Else
            Console.WriteLine("")
            Console.WriteLine("Invalid Size")
        End If
    End Sub

    Public Sub DisplayMatrix()
        If Not IsNothing(A) AndAlso A.Length > 0 Then
            Dim maxLength As Integer = Integer.MinValue
            For i As Integer = 0 To A.GetUpperBound(0)
                For j As Integer = 0 To A.GetUpperBound(1)
                    maxLength = Math.Max(maxLength, A(i, j).ToString.Length)
                Next
            Next

            Console.WriteLine("Matrix Contents:")
            For i As Integer = 0 To A.GetUpperBound(0)
                Console.Write("| ")
                For j As Integer = 0 To A.GetUpperBound(1)
                    Console.Write("{0} ", A(i, j).ToString().PadLeft(maxLength, " "))
                Next
                Console.WriteLine("|")
            Next
        Else
            Console.WriteLine("The Matrix is empty!")
        End If
    End Sub

    Public Sub SaveMatrix()
        If Not IsNothing(A) AndAlso A.Length > 0 Then
            Console.Write("Enter the name of the File to Save To (example: `Matrix.txt`): ")
            Dim response As String = Console.ReadLine()
            Dim fullPathFileName As String = System.IO.Path.Combine(Environment.CurrentDirectory, response)
            Try
                If System.IO.File.Exists(fullPathFileName) Then
                    Console.WriteLine("")
                    Console.WriteLine("There is already a file at:")
                    Console.WriteLine(fullPathFileName)
                    Console.WriteLine("")
                    Console.Write("Would you like to overwrite it? Enter 'Y' or 'YES' to confirm: ")
                    response = Console.ReadLine.ToUpper
                    If response <> "Y" AndAlso response <> "YES" Then
                        Exit Sub
                    End If
                End If
                Using fs As New System.IO.FileStream(fullPathFileName, IO.FileMode.OpenOrCreate)
                    fs.SetLength(0) ' reset file length to 0 in case we are overwriting an existing file
                    Using sw As New System.IO.StreamWriter(fs)
                        Dim line As New System.Text.StringBuilder
                        sw.WriteLine((A.GetUpperBound(0) + 1).ToString()) ' size of array in first line
                        For i As Integer = 0 To A.GetUpperBound(0)
                            line.Clear()
                            For j As Integer = 0 To A.GetUpperBound(1)
                                line.Append(A(i, j).ToString() & " ")
                            Next
                            sw.WriteLine(line.ToString().Trim()) ' output each row to the file
                        Next
                    End Using
                End Using
                Console.WriteLine("")
                Console.WriteLine("Matrix successfully saved to:")
                Console.WriteLine(fullPathFileName)
            Catch ex As Exception
                Console.WriteLine("")
                Console.WriteLine("Error Saving Matrix!")
                Console.WriteLine("Error: " & ex.Message)
            End Try
        Else
            Console.WriteLine("The Matrix is empty!")
        End If
    End Sub

    Public Sub LoadMatrix()
        Console.Write("Enter the name of the File to Load From (example: `Matrix.txt`): ")
        Dim response As String = Console.ReadLine()
        Try
            Dim A2(,) As Integer

            Dim line As String
            Dim value As Integer
            Dim values() As String
            Dim arraySize As Integer
            Dim fullPathFileName As String = System.IO.Path.Combine(Environment.CurrentDirectory, response)
            Using sr As New System.IO.StreamReader(fullPathFileName)
                line = sr.ReadLine ' get matrix size and convert it to an int
                If Integer.TryParse(line, value) Then
                    arraySize = value
                    ReDim A2(arraySize - 1, arraySize - 1)
                    For row As Integer = 0 To arraySize - 1
                        values = sr.ReadLine().Trim().Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
                        If values.Length = arraySize Then
                            For col As Integer = 0 To arraySize - 1
                                If Integer.TryParse(values(col).Trim, value) Then
                                    A2(row, col) = value
                                Else
                                    Console.WriteLine("")
                                    Console.WriteLine("Invalid Element in file!")
                                    Exit Sub
                                End If
                            Next
                        Else
                            Console.WriteLine("")
                            Console.WriteLine("Invalid Row Size in file!")
                            Exit Sub
                        End If
                    Next
                Else
                    Console.WriteLine("")
                    Console.WriteLine("Invalid Matrix Size in first line of file!")
                    Exit Sub
                End If
            End Using
            A = A2
            Console.WriteLine("")
            Console.WriteLine("Matrix successfully loaded from:")
            Console.WriteLine(fullPathFileName)
        Catch ex As Exception
            Console.WriteLine("")
            Console.WriteLine("Error Loading Matrix!")
            Console.WriteLine("Error: " & ex.Message)
        End Try
    End Sub

End Module


回答2:

I have created a class for the Matrix data and the a List(of T) so you con't have to worry about changing the size of an array.

I am using Interpolated strings which is a replacement for String.Format in some cases and easier to read.

I have overriden the .ToString method in the MatrixItem class to make it easy to save the objects to a text file.

Other comments in-line.

Public Sub Main()
    Dim A As List(Of MatrixItem) = CreateListOfMatrixItem()
    SaveMatrixList(A)
    ViewMatrixFile()
    Console.ReadLine()
End Sub

Private Function CreateListOfMatrixItem() As List(Of MatrixItem)
    Dim A As New List(Of MatrixItem)
    Dim HowManyRows As Integer = 3
    For i As Integer = 0 To HowManyRows - 1
        Dim M As New MatrixItem()
        For j As Integer = 0 To 2
            Console.WriteLine($"Enter The Matrix Element at A[Row {i}, Col {j}]: ")
            Select Case j
                Case 0
                    M.X = Convert.ToInt16(Console.ReadLine())
                Case 1
                    M.Y = Convert.ToInt16(Console.ReadLine())
                Case 2
                    M.Z = Convert.ToInt16(Console.ReadLine())
            End Select
        Next
        A.Add(M)
    Next
    Return A
End Function

Private Sub SaveMatrixList(A As List(Of MatrixItem))
    Dim sb As New StringBuilder
    For Each item In A
        sb.AppendLine(item.ToString)
    Next
    'this will open or create the file and append the new data
    'if you want to overwrite the contents of the file then 
    'use File.WriteAllText("Matrix.txt", sb.ToString)
    File.AppendAllText("Matrix.txt", sb.ToString)
End Sub

Private Sub ViewMatrixFile()
    Dim lines = File.ReadAllLines("Matrix.txt")
    Console.WriteLine($"{"X",10}{"Y",10}{"Z",10}")
    For Each line In lines
        Dim numbers = line.Split(","c)
        'To format the values with the a numeric format it is necessary to convert the
        'strings to a numeric type.
        Console.WriteLine($"{CInt(numbers(0)),10:D2}{CInt(numbers(1)),10:D2}{CInt(numbers(2)),10:D2}")
    Next
End Sub

Public Class MatrixItem
    Public Property X As Int16
    Public Property Y As Int16
    Public Property Z As Int16
    Public Overrides Function ToString() As String
        Return $"{X},{Y},{Z}"
    End Function
End Class