Programming Course in C# ¡Free!

Invert binary file - 2

 Saturday, April 06, 2013 published by Exercises C#
Proposed exercise

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.

Output



Solution


using System;
using System.IO;

class InverterFileStream
{
static void Main()
{
Console.Write("Enter the name: ");
string name = Console.ReadLine();

FileStream file = File.OpenRead(name);

long size = file.Length;
byte[] data = new byte[ size ];

file.Read(data,0,(int)size);

file.Close();

FileStream outFile = File.Create(name + ".data");

for (long i = size - 1; i >= 0; i--)
outFile.WriteByte( data[i] );

outFile.Close();
}
}