StreamReader does not read in carriage returns

I need to write a console application for the computer course that I am taking. The program reads the text from the file using StreamReader, splits the string into separate words and stores them in a String array, and then prints the words back.

If there is a carriage return in the file, the file stops reading the text. Can anyone help me with this?

Here is the main program:

using System; using System.IO; using System.Text.RegularExpressions; namespace Assignment2 { class Program { public String[] chop(String input) { input = Regex.Replace(input, @"\s+", " "); input = input.Trim(); char[] stringSeparators = {' ', '\n', '\r'}; String[] words = input.Split(stringSeparators); return words; } static void Main(string[] args) { Program p = new Program(); StreamReader sr = new StreamReader("input.txt"); String line = sr.ReadLine(); String[] splitWords = p.chop(line); for (int i = 1; i <= splitWords.Length; i++) { Console.WriteLine(splitWords[splitWords.Length - i]); } Console.ReadLine(); } } } 

And here is the file "input.txt":

 This is the file you can use to provide input to your program and later on open it inside your program to process the input. 
+4
source share
3 answers

You can use StreamReader.ReadToEnd instead of StreamReader.ReadLine .

 // Cange this: // StreamReader sr = new StreamReader("input.txt"); // String line = sr.ReadLine(); string line; using (StreamReader sr = new StreamReader("input.txt")) { line = sr.ReadToEnd(); } 

Adding a using block ensures that the input file is also closed.

Another alternative:

 string line = File.ReadAllText("input.txt"); // Read the text in one line 

ReadLine reads one line from the file and discards the return characters and line feed characters.

ReadToEnd will read the entire file as a single line and save these characters, allowing your chop method chop work as it is written.

+3
source

You just read on one line. You need to read all the lines to the end of the file. The following should work:

  String line = String.Empty; using (StreamReader sr = new StreamReader("input.txt")) { while (!sr.EndOfStream) { line += sr.ReadLine(); } } 
+3
source

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.

+2
source

Source: https://habr.com/ru/post/1495577/


All Articles