How to limit the lines pulled from the document to 15 lines at a time. Right now it displays all the lines at once. thanks.
class Program {
static void Main(string[] args) {
string[] lines = System.IO.File.ReadAllLines(
@"C:\Users\chri749y\Documents\Skrive til fil\Testprogram.txt");
foreach (string line in lines) {
Console.WriteLine("{0}", line);
}
Console.ReadKey();
}
}
If you want top 15
lines only, try Take (Linq) which is specially designed for this:
var lines = System.IO.File
.ReadLines(@"C:\Users\chri749y\Documents\Skrive til fil\Testprogram.txt")
.Take(15);
In case you want batch processing i.e. get 0 .. 14
lines then 15 .. 29
lines etc.
// Split input into batches with at most "size" items each
private static IEnumerable<T[]> Batch<T>(IEnumerable<T> lines, int size) {
List<T> batch = new List<T>(size);
foreach (var item in lines) {
if (batch.Count >= size) {
yield return batch.ToArray();
batch.Clear();
}
batch.Add(item);
}
if (batch.Count > 0) // tail, possibly incomplete batch
yield return batch.ToArray();
}
Then
var batches = Batch(System.IO.File
.ReadLines(@"C:\Users\chri749y\Documents\Skrive til fil\Testprogram.txt"),
15);
foreach (var batch in batches) { // Array with at most 15 items
foreach (var line in batch) {
...
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With