Untitled

mail@pastecode.io avatar
unknown
csharp
a year ago
1.4 kB
0
Indexable
Never
using System;
using System.IO;
using System.Threading.Tasks;

class Program {
    static void Main(string[] args) {
        string searchString = "example";
        string filePath = "largeFile.txt";
        int chunkSize = 100000; // search 100,000 characters at a time

        // use multiple threads to search for the string
        Parallel.ForEach(Partitioner.Create(0, new FileInfo(filePath).Length), range => {
            using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
                fileStream.Seek(range.Item1, SeekOrigin.Begin);
                using (StreamReader streamReader = new StreamReader(fileStream)) {
                    string text = streamReader.ReadToEnd();
                    int startIndex = range.Item1 > 0 ? text.IndexOf(searchString, StringComparison.OrdinalIgnoreCase) : 0;
                    while (startIndex != -1 && startIndex < range.Item2) {
                        Console.WriteLine("Found string at position " + (startIndex + range.Item1));
                        startIndex = text.IndexOf(searchString, startIndex + 1, StringComparison.OrdinalIgnoreCase);
                    }
                }
            }
        });

        Console.WriteLine("String search completed successfully.");
        Console.ReadLine();
    }
}