Home > Backend Development > C++ > How Can I Efficiently Read a Text File in Reverse Order Using C#?

How Can I Efficiently Read a Text File in Reverse Order Using C#?

Patricia Arquette
Release: 2025-01-29 12:01:20
Original
152 people have browsed it

Read the C#text file method in detail

How Can I Efficiently Read a Text File in Reverse Order Using C#?

In the program design, sometimes the text file needs to be read backward. This can be implemented by iterators, which allows sequential processing data without loading the entire file into memory to improve efficiency.

When reading the text file reverse, the encoding method of the file must be considered. For example, if the file uses UTF-8 coding, you need to correctly identify the starting position of each Unicode character and process any invalid UTF-8 sequence.

The following C#code fragment demonstrates how to use the iterator to read the text file:

<code class="language-csharp">using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;

public static class ReverseFileStreamReader
{
    public static IEnumerable<string> ReadFileLinesReverse(string filePath)
    {
        using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        {
            long fileLength = fileStream.Length;
            byte[] buffer = new byte[1024];

            for (long position = fileLength - buffer.Length; position >= 0; position -= buffer.Length)
            {
                fileStream.Seek(position, SeekOrigin.Begin);
                int bytesRead = fileStream.Read(buffer, 0, buffer.Length);
                var lines = GetLinesFromBuffer(buffer, bytesRead);

                foreach (var line in lines)
                {
                    yield return line;
                }
            }
        }
    }

    private static IEnumerable<string> GetLinesFromBuffer(byte[] buffer, int bytesRead)
    {
        return new string(GetCharactersFromBuffer(buffer, bytesRead))
            .Split('\n')
            .Reverse();
    }

    private static IEnumerable<char> GetCharactersFromBuffer(byte[] buffer, int bytesRead)
    {
        using (var memoryStream = new MemoryStream(buffer, 0, bytesRead))
        {
            using (var streamReader = new StreamReader(memoryStream, Encoding.UTF8))
            {
                while (!streamReader.EndOfStream)
                {
                    yield return (char)streamReader.Read();
                }
            }
        }
    }
}</code>
Copy after login
This code can read the text file reverse and process each line generated in order. It uses the iterator to avoid reading the entire file into memory at one time, thereby improving the efficiency of processing large files. At the same time, it uses to ensure the correct processing of UTF-8 coding files.

The above is the detailed content of How Can I Efficiently Read a Text File in Reverse Order Using C#?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template