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
Example Code
public class Main
{
public static void main(String[] args)
{
BinaryReader inFile = new BinaryReader(File.Open("hola.txt", FileMode.Open));
BinaryWriter outFile = new BinaryWriter(File.Open("hola.txt.inv", FileMode.Create));
long filesize = inFile.BaseStream.getLength();
for (long i = filesize - 1; i >= 0; i--)
{
inFile.BaseStream.Seek(i, SeekOrigin.Begin);
byte b = inFile.ReadByte();
outFile.Write(b);
}
inFile.Close();
outFile.Close();
}
}