gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
read last 10 strings from file using c#
- User
read last 10 strings from file using c#
- Assistant
To read the last 10 strings from a file using C#, you can use the
File.ReadLinesmethod along with LINQ to retrieve the required strings. Here's an example:csharpusing System; using System.IO; using System.Linq; class Program { static void Main(string[] args) { string filePath = "file.txt"; // Replace with your file path // Read the last 10 lines from the file var lines = File.ReadLines(filePath) .Reverse() .Take(10) .Reverse(); // Output the strings foreach (string line in lines) { Console.WriteLine(line); } } }In this example, we're using the
File.ReadLinesmethod to read the lines of the file. We then apply the method to reverse the order of the lines, followed by the method to retrieve the last 10 lines, and finally, we reverse the lines again to get them in the original order. Finally, we iterate over the lines and output them to the console.