File inverter Learn programming Visual Basic (VB.net)

Lesson:

File Management


Exercise:

File inverter


Objetive:

Create a program to "invert" a file: create a file with the same name ending in ".inv" and containing the same bytes as the original file but in reverse order (the first byte will be the last, the second will be the penultimate, and so on, until the last byte of the original file, which should appear in the first position of the resulting file).

You must deliver only the ".cs" file, which should contain a comment with your name.

Hint: To know the length of a binary file (BinaryReader), you can use "myFile.BaseStream.Length" and you can jump to a different position with "myFile.BaseStream.Seek(4, SeekOrigin.Current);"

The starting positions we can use are: SeekOrigin.Begin, SeekOrigin.Current- o SeekOrigin.End


Code:

Imports System
Imports System.IO
Public Class FileInverter
    Public Shared Sub Main()
        Dim inFile As BinaryReader = New BinaryReader(File.Open("hola.txt", FileMode.Open))
        Dim outFile As BinaryWriter = New BinaryWriter(File.Open("hola.txt.inv", FileMode.Create))
        Dim filesize As Long = inFile.BaseStream.Length

        For i As Long = filesize - 1 To 0
            inFile.BaseStream.Seek(i, SeekOrigin.Begin)
            Dim b As Byte = inFile.ReadByte()
            outFile.Write(b)
        Next

        inFile.Close()
        outFile.Close()
    End Sub
End Class

Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.