The problem is that you call ReadLine() , which does just that, it reads until it encounters a carriage return (you have to call it in a loop).
Usually, if you want to read a line of a line by a StreamReader line, the implementation looks more like this (from msdn);
using (StreamReader sr = new StreamReader("TestFile.txt")) { string line; // Read and display lines from the file until the end of // the file is reached. while ((line = sr.ReadLine()) != null) { Console.WriteLine(line); } }
The condition in the while loop ensures that you read to the end of the file, because ReadLine will return null if there is nothing to read.
Another option is to simply use File.ReadAllLines(MyPath) , which will return an array of strings, with each element being one line in the file. To give a more complete example of this:
string[] lines = File.ReadAllLines(MyFilePath); foreach(string line in lines) { string[] words = line.Split(' ').Reverse(); Console.WriteLine(String.Join(" ", words)); }
These three lines of code do the following: Reads the entire file into an array of lines, where each element is a line. Loops over this array, on each line we break it into words and cancel their order. Then I append all the words back together with spaces between them and print them onto the console. If you want the whole file to be in reverse order, you need to start from the last line, not the first, I will leave you this information.
source share