Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read n first Characters of a big Text File - C#

Tags:

c#

I have a very big text file, for example about 1 GB. I need to just read 100 first characters and nothing more.

I searched StackOverflow and other forums but all of them have some solutions which first read the whole file and then will return some n characters of the file.

I do not want to read and load the whole file into memory etc. just need the first characters.

like image 995
Inside Man Avatar asked Dec 05 '22 11:12

Inside Man


2 Answers

You can use StreamReader.ReadBlock() to read a specified number of characters from a file:

public static char[] ReadChars(string filename, int count)
{
    using (var stream = File.OpenRead(filename))
    using (var reader = new StreamReader(stream, Encoding.UTF8))
    {
        char[] buffer = new char[count];
        int n = reader.ReadBlock(buffer, 0, count);

        char[] result = new char[n];

        Array.Copy(buffer, result, n);

        return result;
    }
}

Note that this assumes that your file has UTF8 encoding. If it doesn't, you'll need to specify the correct encoding (in which case you could add an encoding parameter to ReadChars() rather than hard-coding it).

The advantage of using ReadBlock() rather than Read() is that it blocks until either all the characters have been read, or the end of the file has been reached. However, for a FileStream this is of no consequence; just be aware that Read() can return less bytes than asked for in the general case, even if the end of the stream has not been reached.

If you want an async version you can just call ReadBlockAsync() like so:

public static async Task<char[]> ReadCharsAsync(string filename, int count)
{
    using (var stream = File.OpenRead(filename))
    using (var reader = new StreamReader(stream, Encoding.UTF8))
    {
        char[] buffer = new char[count];
        int n = await reader.ReadBlockAsync(buffer, 0, count);

        char[] result = new char[n];

        Array.Copy(buffer, result, n);

        return result;
    }
}

Which you might call like so:

using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;

namespace Demo
{
    static class Program
    {
        static async Task Main()
        {
            string filename = "Your filename here";
            Console.WriteLine(await ReadCharsAsync(filename, 100));
        }
    }
}
like image 198
Matthew Watson Avatar answered Dec 07 '22 23:12

Matthew Watson


Let's read with StreamReader:

  char[] buffer = new char[100];

  using (StreamReader reader = new StreamReader(@"c:\MyFile.txt")) {
    // Technically, StreamReader can read less than buffer.Length characters
    // if the file is too short; 
    // in this case reader.Read returns the number of actually read chars
    reader.Read(buffer, 0, buffer.Length);
  }
like image 26
Dmitry Bychenko Avatar answered Dec 07 '22 23:12

Dmitry Bychenko