seri #7175(2008/08/17 16:55 GMT) [ C# ] Rating0/0=0.00
C#でやってみました。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
using System; using System.Text; using System.Threading; using System.IO; class Tail { private const int SLEEP_TIME = 1000; private static bool fOption = false; private static int outputLines = 10; private static string filename = string.Empty; private static string usage = "Usage: Tail.exe [-n number] [-f] filename"; static void Main(string[] args) { try { for (int i = 0; i < args.Length; i++) { if (args[i][0] == '-') { switch (args[i][1]) { case 'n': outputLines = int.Parse(args[++i]); break; case 'f': fOption = true; break; default: Console.Error.WriteLine("Invalid Option: {0}", args[i]); Console.Error.WriteLine(usage); return; } } else filename = args[i]; } string buffer = string.Empty; int allLines = 0; string line = string.Empty; using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { using (StreamReader sr = new StreamReader(fs, Encoding.Default)) { while (sr.ReadLine() != null) allLines++; fs.Seek(0, SeekOrigin.Begin); if (allLines <= outputLines) buffer = sr.ReadToEnd(); else { for (int i = 0; i < allLines; i++) { line = sr.ReadLine(); if (i > allLines - outputLines - 1) buffer += line + "\n"; } } Console.WriteLine(buffer); while (fOption) { Thread.Sleep(SLEEP_TIME); while ((line = sr.ReadLine()) != null) Console.WriteLine(line); } } } } catch (Exception ex) { Console.Error.WriteLine(ex.Message.ToString()); Console.Error.WriteLine(usage); } } }
Rating0/0=0.00-0+
[ reply ]
seri
#7175()
[
C#
]
Rating0/0=0.00
Rating0/0=0.00-0+
[ reply ]