Difference between revisions of "Headtail.cs"
From AdonaiMedrado.Pro.Br
(New page: <code lang="csharp"> using System; using System.Collections.Generic; using System.Text; using System.IO; namespace headtail { class Program { static void Main(string[] arg...) |
|||
Line 1: | Line 1: | ||
+ | == Versão 1 == | ||
<code lang="csharp"> | <code lang="csharp"> | ||
using System; | using System; | ||
Line 40: | Line 41: | ||
Console.WriteLine(linhas[x]); | Console.WriteLine(linhas[x]); | ||
r.Close(); | r.Close(); | ||
+ | } | ||
+ | catch (Exception ex) | ||
+ | { | ||
+ | Console.WriteLine("> " + s + ": " + ex.Message); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </code> | ||
+ | == Versão 2 == | ||
+ | <code lang="csharp"> | ||
+ | using System; | ||
+ | using System.Collections.Generic; | ||
+ | using System.Text; | ||
+ | using System.IO; | ||
+ | |||
+ | namespace headtail | ||
+ | { | ||
+ | class Program | ||
+ | { | ||
+ | static void Main(string[] args) | ||
+ | { | ||
+ | bool? head = null; | ||
+ | if (args[0] == "-tail" || args[0] == "-t") | ||
+ | head = false; | ||
+ | else if (args[0] == "-head" || args[0] == "-h") | ||
+ | head = true; | ||
+ | |||
+ | if (head==null || args.Length < 2) | ||
+ | { | ||
+ | Console.WriteLine("Usage: headtail <-tail|-t|-head|-h> <file1>[...<fileN>]"); | ||
+ | return; | ||
+ | } | ||
+ | |||
+ | for (int i = 1; i < args.Length; i++) | ||
+ | { | ||
+ | string s = args[i]; | ||
+ | FileStream r = null; | ||
+ | try | ||
+ | { | ||
+ | r = File.OpenRead(s); | ||
+ | int porcentagem = (int)(r.Length*.2); | ||
+ | |||
+ | if (args.Length > 3) | ||
+ | Console.WriteLine("> " + s); | ||
+ | |||
+ | byte[] buf=new byte[porcentagem]; | ||
+ | if (!(bool)head) | ||
+ | r.Seek(-porcentagem,SeekOrigin.End); | ||
+ | r.Read(buf,0,buf.Length); | ||
+ | |||
+ | string[] linhas = ASCIIEncoding.Default.GetString(buf).Split(new char[] { '\n' }); | ||
+ | |||
+ | for (int x = 1; x < linhas.Length; x++) | ||
+ | Console.WriteLine(linhas[x]); | ||
+ | r.Close(); | ||
+ | r.Dispose(); | ||
} | } | ||
catch (Exception ex) | catch (Exception ex) |
Revision as of 17:51, 19 May 2009
Versão 1
using System; using System.Collections.Generic; using System.Text; using System.IO; namespace headtail { class Program { static void Main(string[] args) { bool? head = null; if (args[0] == "-tail" || args[0] == "-t") head = false; else if (args[0] == "-head" || args[0] == "-h") head = true; if (head==null || args.Length < 2) { Console.WriteLine("Usage: headtail <-tail|-t|-head|-h> <file1>[...<fileN>]"); return; } for (int i = 1; i < args.Length; i++) { string s = args[i]; try { TextReader r = File.OpenText(s); if (args.Length > 3) Console.WriteLine("> " + s); string[] linhas = r.ReadToEnd().Split(new char[] { '\n' }); if ((bool)head) for (int x = 0; x < (int)(linhas.Length * .2); x++) Console.WriteLine(linhas[x]); else for (int x = (int)(linhas.Length * .7); x < linhas.Length; x++) Console.WriteLine(linhas[x]); r.Close(); } catch (Exception ex) { Console.WriteLine("> " + s + ": " + ex.Message); } } } } }
Versão 2
using System; using System.Collections.Generic; using System.Text; using System.IO; namespace headtail { class Program { static void Main(string[] args) { bool? head = null; if (args[0] == "-tail" || args[0] == "-t") head = false; else if (args[0] == "-head" || args[0] == "-h") head = true; if (head==null || args.Length < 2) { Console.WriteLine("Usage: headtail <-tail|-t|-head|-h> <file1>[...<fileN>]"); return; } for (int i = 1; i < args.Length; i++) { string s = args[i]; FileStream r = null; try { r = File.OpenRead(s); int porcentagem = (int)(r.Length*.2); if (args.Length > 3) Console.WriteLine("> " + s); byte[] buf=new byte[porcentagem]; if (!(bool)head) r.Seek(-porcentagem,SeekOrigin.End); r.Read(buf,0,buf.Length); string[] linhas = ASCIIEncoding.Default.GetString(buf).Split(new char[] { '\n' }); for (int x = 1; x < linhas.Length; x++) Console.WriteLine(linhas[x]); r.Close(); r.Dispose(); } catch (Exception ex) { Console.WriteLine("> " + s + ": " + ex.Message); } } } } }