1

This is my code.

        string FileName = @"File.txt";    

        if (File.Exists(FileName))
        {
            FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);

            BinaryReader br = new BinaryReader(fs);

            for (long i = fs.Length; i > 0; i--)
            {
                Console.Write(Convert.ToChar(br.Read()));
            }

        }
        else
        {

But it still gives me the same output.. it reads the file in right order from start to end. I want it to read from last to first.

PROBLEM SOLVED Final Code

string FileName = @"File.txt";

        if (File.Exists(FileName))
        {
            FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);
            int length = (int)fs.Length;

            BinaryReader br = new BinaryReader(fs);

            byte[] myArray = br.ReadBytes((int)fs.Length);

            for (long i = myArray.Length - 1; i > 0; i--)
            {
                Console.Write(Convert.ToChar(myArray[i]));
            }
            Console.WriteLine();
        }
        else
        {
            Console.WriteLine("File Not Exists");
        }
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
LeCdr
  • 323
  • 1
  • 5
  • 18
  • Shouldn't that be i >= 0, I think you will my last character if you intend to read every byte. – tony95 Feb 28 '22 at 21:29

5 Answers5

3

Depending on the file's size, the following solution is going to consume a lot of memory, since it will hold two copies of the file's contents in memory. But it should be OK with smaller files (think sizes less than aprox. 10 MB) and is easy to understand:

// using System;
// using System.IO;

byte[] fileContents = File.ReadAllBytes(filePath);
Array.Reverse(fileContents);

… // process `fileContents` instead of an input file stream

P.S.: If you want to process larger files (ones of many MB in size), you should not use this solution, as it could quickly lead to OutOfMemoryExceptions. I would suggest that you read reasonably-sized (e.g. 64 KB) chunks of the file into memory (starting near the end and proceeding towards the beginning the file, by performing seeks) and processing these one-by-one.

Fidel
  • 7,027
  • 11
  • 57
  • 81
stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
1

You need to use different Read method (Read(byte[]buffer, int index, int length)) of BinaryReader to specify start index and length of data you want to read.

for(long i = fs.Length; i > 0; i--)
{
    byte[] buffer = new byte[1];
    br.Read(buffer, i, 1);
    Console.Write(Convert.ToChar(buffer[0]));
}

EDIT: My approach was not good, if you would like to user Read(byte[], int, int) method, you should first read whole file, and then reverse the array, as @Crowcoder mentioned in his solution.

tdragon
  • 3,209
  • 1
  • 16
  • 17
0

Just read all of the bytes in the stream, save that to a byte list and reverse it like so:

List<byte> data = new List<byte>();
for (int i = 0; i < br.BaseStream.Length; ++i)
  data.Add(br.ReadByte());

data.Reverse();
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
0

Use ReadBytes(fs.Length) to get an array of bytes then use your loop on the array.

Crowcoder
  • 11,250
  • 3
  • 36
  • 45
0

I've posted an answer for reversing a small file here.

If you need to reverse a large file without consuming your memory, there is a separate answer here.

Fidel
  • 7,027
  • 11
  • 57
  • 81