Go to C # file line

how can I go to some line in my file, for example, line 300 in c: \ text.txt?

+6
c # file-io
source share
5 answers
Dim arrText() As String Dim lineThreeHundred As String arrText = File.ReadAllLines("c:\test.txt") lineThreeHundred = arrText(299) 

Edit: C # version

 string[] arrText; string lineThreeHundred; arrText = File.ReadAllLines("c:\test.txt"); lineThreeHundred = arrText[299]; 
+1
source share
 using (var reader = new StreamReader(@"c:\test.txt")) { for (int i = 0; i < 300; i++) { reader.ReadLine(); } // Now you are at line 300. You may continue reading } 
+12
source share

Line delimited files are not intended for random access. Thus, you need to search for a file by reading and discarding the required number of lines.

Modern approach:

 class LineReader : IEnumerable<string>, IDisposable { TextReader _reader; public LineReader(TextReader reader) { _reader = reader; } public IEnumerator<string> GetEnumerator() { string line; while ((line = _reader.ReadLine()) != null) { yield return line; } } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } public void Dispose() { _reader.Dispose(); } } 

Using:

 // path is string int skip = 300; StreamReader sr = new StreamReader(path); using (var lineReader = new LineReader(sr)) { IEnumerable<string> lines = lineReader.Skip(skip); foreach (string line in lines) { Console.WriteLine(line); } } 

Simple approach:

 string path; int count = 0; int skip = 300; using (StreamReader sr = new StreamReader(path)) { while ((count < skip) && (sr.ReadLine() != null)) { count++; } if(!sr.EndOfStream) Console.WriteLine(sr.ReadLine()); } } 
+9
source share

A few things I noticed:

  • Microsoft use case StreamReader constructor checks whether a file exists.

  • You must notify the user through a message on the screen or in the log, if the file either does not exist or is shorter than we expected. This allows you to be aware of any unexpected errors if they occur while you are debugging other parts of the system. I understand that this was not part of your original question, but a good practice.

So this is a combination of several other answers.

 string path = @"C:\test.txt"; int count = 0; if(File.Exists(path)) { using (var reader = new StreamReader(@"c:\test.txt")) { while (count < 300 && reader.ReadLine() != null) { count++; } if(count != 300) { Console.WriteLine("There are less than 300 lines in this file."); } else { // keep processing } } } else { Console.WriteLine("File '" + path + "' does not exist."); } 
+1
source share
 /// <summary> /// Gets the specified line from a text file. /// </summary> /// <param name="lineNumber">The number of the line to return.</param> /// <param name="path">Identifies the text file that is to be read.</param> /// <returns>The specified line, is it exists, or an empty string otherwise.</returns> /// <exception cref="ArgumentException">The line number is negative, or the path is missing.</exception> /// <exception cref="System.IO.IOException">The file could not be read.</exception> public static string GetNthLineFromTextFile(int lineNumber, string path) { if (lineNumber < 0) throw new ArgumentException(string.Format("Invalid line number \"{0}\". Must be greater than zero.", lineNumber)); if (string.IsNullOrEmpty(path)) throw new ArgumentException("No path was specified."); using (System.IO.StreamReader reader = new System.IO.StreamReader(path)) { for (int currentLineNumber = 0; currentLineNumber < lineNumber; currentLineNumber++) { if (reader.EndOfStream) return string.Empty; reader.ReadLine(); } return reader.ReadLine(); } } 
0
source share

All Articles