C#从文件或标准输入设备读取指定行的方法
更新时间:2020年6月25日 11:31 点击:2158
本文实例讲述了C#从文件或标准输入设备读取指定行的方法。分享给大家供大家参考。具体如下:
using System; using System.IO; using System.Runtime.InteropServices; using System.Text.RegularExpressions; using System.Collections.Generic; namespace RobvanderWoude { class ReadLine { static int Main( string[] args ) { #region Command Line Parsing string filename = string.Empty; int linestart = 1; int lineend = 2; bool concat = false; bool addspaces = false; string concatchar = string.Empty; bool skipempty = false; bool trimlines = false; bool numlines = false; bool redirected; bool set_c = false; bool set_l = false; bool set_s = false; bool set_t = false; bool set_input = false; if ( ConsoleEx.InputRedirected ) { set_input = true; redirected = true; } else { if ( args.Length == 0 ) { return WriteError( ); } redirected = false; } foreach ( string arg in args ) { if ( arg[0] == '/' ) { try { switch ( arg.ToUpper( )[1] ) { case '?': return WriteError( ); case 'C': if ( arg.ToUpper( ) != "/C" && arg.ToUpper( ) != "/CS" ) { return WriteError( "Invalid command line switch " + arg ); } concat = true; if ( arg.ToUpper( ) == "/CS" ) { addspaces = true; } if ( set_c ) { return WriteError( "Duplicate command line argument /C*" ); } set_c = true; break; case 'L': if(arg.ToUpper( ).StartsWith( "/L:" ) && arg.Length > 3) { if ( arg[2] == ':' ) { string linessel = arg.Substring( 3 ); string pattern = @"^(\-?\d+)$"; Match match = Regex.Match( linessel, pattern ); if ( match.Success ) { linestart = Convert.ToInt32( match.Groups[1].Value ); lineend = linestart + 1; } else { pattern = @"^(\-?\d+)\.\.(\-?\d+)$"; match = Regex.Match( linessel, pattern ); if ( match.Success ) { linestart = Convert.ToInt32( match.Groups[1].Value ); lineend = Convert.ToInt32( match.Groups[2].Value ) + 1; } else { pattern = @"^(\-?\d+),(\-?\d+)$"; match = Regex.Match( linessel, pattern ); if ( match.Success ) { // numlines is true if the second number //specifies the number of lines instead of a line number numlines = true; linestart = Convert.ToInt32( match.Groups[1].Value ); lineend = Convert.ToInt32( match.Groups[2].Value ); if ( lineend < 1 ) { return WriteError( "Invalid number of lines (" + lineend.ToString( ) + "), must be 1 or higher" ); } } } } } else { return WriteError( "Invalid command line switch " + arg ); } } else { return WriteError( "Invalid command line switch " + arg ); } if ( set_l ) { return WriteError( "Duplicate command line argument /L" ); } set_l = true; break; case 'S': if ( arg.ToUpper( ) != "/SE" ) { return WriteError( "Invalid command line switch " + arg ); } skipempty = true; if ( set_s ) { return WriteError( "Duplicate command line argument /SE" ); } set_s = true; break; case 'T': if ( arg.ToUpper( ) != "/T" ) { return WriteError( "Invalid command line switch " + arg ); } trimlines = true; if ( set_t ) { return WriteError( "Duplicate command line argument /T" ); } set_t = true; break; default: return WriteError( "Invalid command line switch " + arg ); } } catch { return WriteError( "Invalid command line switch " + arg ); } } else { if ( set_input ) { return WriteError( "Multiple inputs specified (file + redirection or multiple files)" ); } if ( redirected ) { return WriteError( "Do not specify a file name when using redirected input" ); } else { filename = arg; } } } #endregion try { int count = 0; bool output = false; string[] lines; List<string> alllines = new List<string>( ); if ( redirected ) { // Read standard input and store the lines in a list int peek = 0; do { alllines.Add( Console.In.ReadLine( ) ); } while ( peek != -1 ); // Convert the list to an array lines = alllines.ToArray( ); } else { // Read the file and store the lines in a list lines = File.ReadAllLines( filename ); } // Check if negative numbers were used, and if so, //calculate the resulting line numbers if ( linestart < 0 ) { linestart += lines.Length + 1; } if ( lineend < 0 ) { lineend += lines.Length + 1; } if ( numlines ) { lineend += linestart; } // Iterate through the array of lines and display //the ones matching the command line switches foreach ( string line in lines ) { string myline = line; if ( trimlines ) { myline = myline.Trim( ); } bool skip = skipempty && ( myline.Trim( ) == string.Empty ); if ( !skip ) { count += 1; if ( count >= linestart && count < lineend ) { if ( concat ) { Console.Write( "{0}{1}", concatchar, myline ); } else { Console.WriteLine( myline ); } if ( addspaces ) { concatchar = " "; } } } } } catch ( Exception e ) { return WriteError( e.Message ); } return 0; } #region Redirection Detection public static class ConsoleEx { public static bool OutputRedirected { get { return FileType.Char != GetFileType( GetStdHandle( StdHandle.Stdout ) ); } } public static bool InputRedirected { get { return FileType.Char != GetFileType( GetStdHandle( StdHandle.Stdin ) ); } } public static bool ErrorRedirected { get { return FileType.Char != GetFileType( GetStdHandle( StdHandle.Stderr ) ); } } // P/Invoke: private enum FileType { Unknown, Disk, Char, Pipe }; private enum StdHandle { Stdin = -10, Stdout = -11, Stderr = -12 }; [DllImport( "kernel32.dll" )] private static extern FileType GetFileType( IntPtr hdl ); [DllImport( "kernel32.dll" )] private static extern IntPtr GetStdHandle( StdHandle std ); } #endregion #region Error Handling public static int WriteError( Exception e = null ) { return WriteError( e == null ? null : e.Message ); } public static int WriteError( string errorMessage ) { if ( string.IsNullOrEmpty( errorMessage ) == false ) { Console.Error.WriteLine( ); Console.ForegroundColor = ConsoleColor.Red; Console.Error.Write( "ERROR: " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.WriteLine( errorMessage ); Console.ResetColor( ); } Console.Error.WriteLine( ); Console.Error.WriteLine( "ReadLine, Version 0.30 beta" ); Console.Error.WriteLine( "Return the specified line(s) from a file or Standard Input" ); Console.Error.WriteLine( ); Console.Error.Write( "Usage: " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.WriteLine( "READLINE filename [ options ]" ); Console.ResetColor( ); Console.Error.Write( " or: " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.WriteLine( "READLINE [ options ] < filename" ); Console.ResetColor( ); Console.Error.Write( " or: " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.WriteLine( "command | READLINE [ options ]" ); Console.ResetColor( ); Console.Error.WriteLine( ); Console.Error.Write( "Where: " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( "filename" ); Console.ResetColor( ); Console.Error.WriteLine( " is the optional file to be read" ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( " command" ); Console.ResetColor( ); Console.Error.WriteLine( " is the optional command whose output is to be read" ); Console.Error.WriteLine( ); Console.Error.Write( "Options: " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( "/C C" ); Console.ResetColor( ); Console.Error.WriteLine( "oncatenate lines" ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( " /CS C" ); Console.ResetColor( ); Console.Error.Write( "oncatenate lines with " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( "S" ); Console.ResetColor( ); Console.Error.WriteLine( "paces in between" ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( " /L:n" ); Console.ResetColor( ); Console.Error.Write( " read line " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.WriteLine( "n" ); Console.ResetColor( ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( " /L:n..m" ); Console.ResetColor( ); Console.Error.Write( " read lines " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( "n" ); Console.ResetColor( ); Console.Error.Write( " through " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.WriteLine( "m" ); Console.Error.Write( " /L:n,m" ); Console.ResetColor( ); Console.Error.Write( " read " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( "m" ); Console.ResetColor( ); Console.Error.Write( " lines starting at line " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.WriteLine( "n" ); Console.ResetColor( ); Console.Error.WriteLine( "(negative numbers start counting from the end backwards)" ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( " /SE S" ); Console.ResetColor( ); Console.Error.Write( "kip " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( "E" ); Console.ResetColor( ); Console.Error.WriteLine( "mpty lines" ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( " /T T" ); Console.ResetColor( ); Console.Error.WriteLine( "rim leading and trailing whitespace from lines" ); Console.Error.WriteLine( ); Console.Error.WriteLine( "Examples:" ); Console.Error.WriteLine( "READLINE file read the first non-empty line (default)" ); Console.Error.WriteLine( "READLINE file /L:2 /SE read the second non-empty line of file" ); Console.Error.WriteLine( "READLINE file /L:5..7 read lines 5..7 of file" ); Console.Error.WriteLine( "READLINE file /L:-1 read the last line of file" ); Console.Error.WriteLine( "READLINE file /L:-2..-1 read the last 2 lines of file" ); Console.Error.WriteLine( "READLINE file /L:-2,2 read the last 2 lines of file" ); Console.Error.WriteLine( ); Console.Error.Write( "Check for redirection by Hans Passant on " ); Console.ForegroundColor = ConsoleColor.DarkGray; Console.Error.WriteLine( "StackOverflow.com" ); Console.Error.WriteLine( "/questions/3453220/how-to-detect-if-console-in-stdin-has-been-redirected" ); Console.ResetColor( ); Console.Error.WriteLine( ); Console.Error.WriteLine( "Written by Rob van der Woude" ); return 1; } #endregion } }
希望本文所述对大家的C#程序设计有所帮助。
上一篇: C#中数组初始化、反转和排序用法实例
下一篇: C#中简单的拆箱操作用法实例分析
相关文章
- 下面小编来给大家演示几个php操作zip文件的实例,我们可以读取zip包中指定文件与删除zip包中指定文件,下面来给大这介绍一下。 从zip压缩文件中提取文件 代...2016-11-25
- 我们在使用C#做项目的时候,基本上都需要制作登录界面,那么今天我们就来一步步看看,如果简单的实现登录界面呢,本文给出2个例子,由简入难,希望大家能够喜欢。...2020-06-25
- 这篇文章主要介绍了C# 字段和属性的的相关资料,文中示例代码非常详细,供大家参考和学习,感兴趣的朋友可以了解下...2020-11-03
- 这篇文章主要介绍了C#中截取字符串的的基本方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-03
- 本文给大家分享C#连接SQL数据库和查询数据功能的操作技巧,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友参考下吧...2021-05-17
Jupyter Notebook读取csv文件出现的问题及解决
这篇文章主要介绍了JupyterNotebook读取csv文件出现的问题及解决,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2023-01-06- 这篇文章主要介绍了C#实现简单的Http请求的方法,以实例形式较为详细的分析了C#实现Http请求的具体方法,需要的朋友可以参考下...2020-06-25
- 本文主要介绍了C#中new的几种用法,具有很好的参考价值,下面跟着小编一起来看下吧...2020-06-25
使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序)
这篇文章主要介绍了使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25- 这篇文章主要介绍了C#开发Windows窗体应用程序的简单操作步骤,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-12
- 这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
- 最近做一个小项目不可避免的需要前端脚本与后台进行交互。由于是在asp.net中实现,故问题演化成asp.net中jiavascript与后台c#如何进行交互。...2020-06-25
- 这篇文章主要用实例讲解C#递归算法的概念以及用法,文中代码非常详细,帮助大家更好的参考和学习,感兴趣的朋友可以了解下...2020-06-25
- 本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
- 轻松学习C#的基础入门,了解C#最基本的知识点,C#是一种简洁的,类型安全的一种完全面向对象的开发语言,是Microsoft专门基于.NET Framework平台开发的而量身定做的高级程序设计语言,需要的朋友可以参考下...2020-06-25
- 本文主要介绍了C#变量命名规则小结,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-09
- 这篇文章主要介绍了c#中(&&,||)与(&,|)的区别详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
- 本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
- 这篇文章主要介绍了C#绘制曲线图的方法,以完整实例形式较为详细的分析了C#进行曲线绘制的具体步骤与相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了c#自带缓存使用方法,包括获取数据缓存、设置数据缓存、移除指定数据缓存等方法,需要的朋友可以参考下...2020-06-25