Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
GOOD EVENING
I want to read a very larg one terrabyte file using the LIFO (last in first out) method.
The current code uses the FIFO method.
How can this code be modified to do LIFO for a very large terrabyte files?
THANK YOU

What I have tried:

C#
using System;
using System.IO;
using System.Collections;

namespace Applica
{
    static class Program
    {
        static void Main(string[] args)
        {
            DirectoryInfo da = new DirectoryInfo("C:\\Fol");
            if (!da.Exists)
            {
                Console.WriteLine("The folder '{0}' does not exist.", da.FullName);
                return;
            }
            FileInfo[] Arr = da.GetFiles();
            if (Arr.Length == 0)
            {
                Console.WriteLine("There are no files in the folder '{0}'.", da.FullName);
                return;
            }
            FileInfo ap = Arr[Arr.Length - 1];
            long Totbyte = ap.Length;
            string filePath = ap.FullName;
            Console.WriteLine("Total Bytes = {0} bytes", Totbyte);

            const int BufferSize = 1024;
            byte[] buffer = new byte[BufferSize];

            string destinationPath = Path.Combine("C:\\check", Path.GetFileName(filePath));

            using (Stream input = File.OpenRead(filePath))
            using (Stream output = File.OpenWrite(destinationPath))
            {
                int bytesRead;
                while ((bytesRead = input.Read(buffer, 0, BufferSize)) > 0)
                {
                    for (int count = 0; count < bytesRead; count++)
                    {
                        byte theByte = buffer[count];
                        string theByteInBinary = Convert.ToString(theByte, 2).PadLeft(8, '0');
                        Console.WriteLine("{0} = {1}", theByteInBinary, theByte);
                    }
                    output.Write(buffer, 0, bytesRead);
                }
            }
        }
    }
}
Posted
Updated 7-Jan-17 4:14am
v2
Comments
[no name] 6-Jan-17 20:08pm    
You seek to the end of the file and read backwards.
Jon McKee 6-Jan-17 23:57pm    
I wish I could upvote a comment. This is exactly the solution.
Patrice T 7-Jan-17 10:34am    
What the size of the data ? Bytes? words? Lines.

1 solution

You Seek to the last byte in the file and read that byte. Now the file pointer is sitting after that byte. To read the next byte (before the byte you just read) you have to Seek backwards two bytes and read one byte again. Do this until you're finished or you try to Seek to -1.

Warning! This is a very inefficient method of reading the file and will likely take forever on a 1TB file.

A better method would be to read blocks of bytes into memory and process the bytes in those blocks, byte-by-byte or however you need to.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900