Flat File Line Counter
Console application count lines - usefulin very long .txt files. Also reads 10 first and 10 last lines and show them with line numbers in console!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace LineCount
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < args.Length; i++)
{
FileStream file =
new FileStream(args[i], FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(file);
String s = "";
int k = 0, max;
Console.WriteLine("------ " + args[i] + " 10 first lines ");
while (!sr.EndOfStream)
{
s = sr.ReadLine();
if (k < 10)
{
Console.WriteLine((k + 1) + " " + s);
}
k++;
}
max = k;
file.Seek(0L, SeekOrigin.Begin);
Console.WriteLine("------ and 10 last lines");
k = 0;
while (!sr.EndOfStream)
{
s = sr.ReadLine();
if (k >= max - 10)
{
Console.WriteLine((k + 1) + " " + s);
}
k++;
}
Console.WriteLine("------ " + args[i] + " length " + max + " lines ");
Console.WriteLine("----------------------------------------");
sr.Close();
file.Close();
}
if (args.Length > 0)
{
Console.WriteLine("Press any key!");
Console.ReadKey();
}
}
}
}
Example usage: linecount.exe bigfile1.txt anotherbigfile.dat > fsizes.log
2012.11.22 22:29:16.