8.23 - Invert binary file - 2

Objetive

Create a program to "invert" a file, using a "FileStream": 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


Solution

using System;
using System.IO;

class InverterFileStream
{
    static void Main(string[] args)
    {
        string fileName;

        Console.Write("Enter the name of file to convert: ");
        fileName = Console.ReadLine();
        
        FileStream myFileReader = File.OpenRead(fileName);
        
        long size = myFileReader.Length;
        byte[] data = new byte[ size ];
        myFileReader.Read(data,0,(int)size);
        myFileReader.Close();
                    
        FileStream myFileWriter = File.Create(fileName + ".inv");
     
        for (long i=size-1; i >= 0; i--)
            myFileWriter.WriteByte( data[i] );
        
        myFileWriter.Close();
    }
}


Popular Posts